Comparing array from user to array from database PHP -


i working on adding product module.

the user can upload csv file add products. how can create system if there existing product in db, skip product , check again product. if there no such product in db, product added db.

so let's

//products in db  //code       //description 1            item 1 2            item 2 3            item 3  //products in user input //code       //description 1            item 1  3            item 3 4            item 4 5            item 5 

from above example, how can create system add item 4 , item 5 db?

here $db_products array contains products db

foreach($db_products $prod){   $products[] = array(     "code" => $prod['code'],     "description" => $prod['description']   ); } 

here $products_array contains user input

foreach($products $v){     $products_array[] = array(         "code"        => $v['code'],         "description" => $v['description'],     ); } 

i used code compare

foreach($products_array $prod){   foreach($db_products $db_prod){     $result = array_diff($db_prod, $prod);         var_dump($result);   } } 

but why returns me same data between arrays?

what missing here?? thank you

you can use recursive function check if $prod $products_array exists in multidimensional array $db_products, this:

$products_to_add = array(); foreach ($products_array $prod) {     if (!inmultiarray($prod, $db_products)) {         $products_to_add[] = $prod; // add product that's -not- in db $products_to_add     } }  function inmultiarray($needle, $haystack, $strict = false) {     foreach ($haystack $item) {         if (is_object($item)) {             $item = get_object_vars($item);         }          if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && inmultiarray($needle, $item, $strict))) {             return true;         }     }      return false; } 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -