php - array_key_exists does not find the second array element -


in short - have 2 $ xml , $ db arrays, both have values code.

i foreach first one, i'm downloading code , looking in second $db. if $db contains code - i'll display information exists, if not - not. @ least - because instead of information key in array, else

$xml = array(     array(         "code" => 456,     ),     array(         "code" => 789,     ), );  $db = array(     array(         "code" => 456,     ),     array(         "code" => 789,     ), );  foreach ($xml $product) {       if (array_key_exists($product['code'], $db)) {          echo "key in db array";      } else {          echo "key isn't in db array";     }  } 

you trying check value of 1 array values of array.

so need in_array array_column below:-

explanation:-

1.in_array search value exist in array or not.

2.array_column give single-dimensional array based on index-name given multi-dimensional array.

3.so code become this:- if (in_array(456, array(0 => 456,1 => 789)){

and work fine.

$db_array = array_column($db,'code'); foreach ($xml $product) {     if (in_array($product['code'],$db_array )) {         echo "product code ".$product['code']." in db array\n";     } else {         echo "product code ".$product['code']." isn't in db array\n";     } } 

output:-https://eval.in/860191


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -