简体   繁体   中英

php associative array : compare adjacent values

I would like to check many rows of the same 2 input fields in a form. The validation should fail if one is empty and the other is not.

I have created an associative array based on several input fields(e_me_id, e_md_number, e_md_id1,e_md_number1, p_me_id,p_md_number...) in a form.

$pattern='(md_number|me_id)';
foreach($_POST as $field => $value) {
    $success = preg_match($pattern, $field);
    if ($success) {

        $validate += [$field => $value];
        }   
}

 result of validate =(
[e_me_id] => 1 
[e_md_number] => 111 
[e_me_id2] => 2 
[e_md_number2] => 222 
[p_me_id] => 10 
[p_md_number] => 101010 
[f_me_id] => 16 
[f_md_number] => 161616 
[d_me_id] => 18 
[d_md_number] => 181818 )

I need some looping php to check that the first/second are both null or both filled... same for third/forth, fifth/sixth... etc etc.

I tried to use prev($validate) and next($validate) but could not get it to work.

Any ideas or a different approach.

Thanks in advance.

You can do this to do the validation of pairs:

// assuming this is the array generated by your code ...
$tst= array("e_me_id" => 1,"e_md_number" => 111 ,
            "e_me_id2" => 2,"e_md_number2" => 222 ,
            "p_me_id" => 0,"p_md_number" => 101010 ,
            "f_me_id" => 16,"f_md_number" => 161616 ,
            "d_me_id" => 0,"d_md_number" => 0 );
// then this will do the validation of pairs:
$keys=array_keys($tst);
for ($i=0;$i<count($tst);$i+=2) 
  echo "$keys[$i] and "
    .$keys[$i+1]
    .(empty($tst[$keys[$i]]) == empty($tst[$keys[$i+1]])?'':' DO NOT')
    ." pass the validation.\n";

You can see a demo here: https://rextester.com/VEMUFJ14979
I changed a few of the numbers to demonstrate the different possible cases.

Using the == operator between the two empty() -tests is equivalent to using the negated xor operator as suggested by @Markus Zeller.

Edit :

A "shorthand-if statement" should look something like this:

$x = (empty($tst[$keys[$i]]) == empty($tst[$keys[$i+1]])?'':$fieldname);

Although to me it is not quite clear what you intend $x and $fieldname to be in your for -loop. This would set $x to '' if case both tested values were empty or both were "filled" and it would set it to $fieldname when only one of them was empty.

Your most recent comment led me to believe that you want to have an overall validation result in $x. In order to get this you will have to count all errors while looping over the pairs. Something like the following should do that:

for ($i=0,$x=0;$i<count($tst);$i+=2){
  if (empty($tst[$keys[$i]]) != empty($tst[$keys[$i+1]])) $x++;
  // echo $keys[$i].", error count so far: $x\n";
}

echo ("here ".($x?'validation error!':'OK'));

See the demo here: https://rextester.com/XTK88037

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM