Thursday, 30 August 2018

How to compare two multidimensional arrays and add a variable to one if the values ​​are found?

I have a shopping cart and I use two array: one for getting the cart products from session, and one to display the products. I need to get the count value from cart array, to corespondent product from products array. I need the count value in products->barrels array, becouse I will use a placeholder to show existing value.

The chemID and catID values makes the product unique.
I don't want to change the array structure, only to add the count value to products array....Please help
Cart array:
 array(2) {
  [0]=>
  object(stdClass)#224 (9) {
    ["chemID"]=>
    string(3) "657"
    ["product_number"]=>
    string(8) "14004015"
    ["size"]=>
    string(6) "15 GAL"
    ["catID"]=>
    string(2) "24"
    ["list_price"]=>
    string(6) "459.00"
    ["count"]=>
    string(1) "2"
    ["attribute"]=>
    string(6) "Yellow"
  }
  [1]=>
  object(stdClass)#225 (9) {
    ["chemID"]=>
    string(3) "658"
    ["product_number"]=>
    string(9) "14004015C"
    ["size"]=>
    string(6) "15 GAL"
    ["catID"]=>
    string(2) "24"
    ["list_price"]=>
    string(3) "434"
    ["count"]=>
    string(1) "3"
  }
}

Products array:
 array(2) {
  [657]=>
  array(4) {
    ["attribute"]=>
    string(6) "Yellow"
    ["barrels"]=>
    array(3) {
      [0]=>
      object(stdClass)#293 (9) {
        ["product_number"]=>
        string(8) "14004005"
        ["size"]=>
        string(5) "5 Gal"
        ["catID"]=>
        string(2) "13"
        ["list_price"]=>
        string(6) "169.00"
        ["chemID"]=>
        string(3) "657"
        ["attribute"]=>
        string(6) "Yellow"
      }
      [1]=>
      object(stdClass)#294 (9) {
        ["product_number"]=>
        string(8) "14004015"
        ["size"]=>
        string(6) "15 GAL"
        ["catID"]=>
        string(2) "24"
        ["list_price"]=>
        string(6) "459.00"
        ["chemID"]=>
        string(3) "657"
        ["attribute"]=>
        string(6) "Yellow"
      }
      [2]=>
      object(stdClass)#295 (9) {
        ["product_number"]=>
        string(8) "14004030"
        ["size"]=>
        string(6) "30 Gal"
        ["catID"]=>
        string(1) "2"
        ["list_price"]=>
        string(6) "874.00"
        ["chemID"]=>
        string(3) "657"
        ["attribute"]=>
        string(6) "Yellow"
      }
    }
  }
  [658]=>
  array(4) {
    ["attribute"]=>
    string(5) "Clear"
    ["barrels"]=>
    array(3) {
      [0]=>
      object(stdClass)#296 (9) {
        ["product_number"]=>
        string(9) "14004005C"
        ["size"]=>
        string(5) "5 Gal"
        ["catID"]=>
        string(2) "13"
        ["list_price"]=>
        string(6) "159.00"
        ["chemID"]=>
        string(3) "658"
        ["attribute"]=>
        string(5) "Clear"
      }
      [1]=>
      object(stdClass)#297 (9) {
        ["product_number"]=>
        string(9) "14004015C"
        ["size"]=>
        string(6) "15 GAL"
        ["catID"]=>
        string(2) "24"
        ["list_price"]=>
        string(3) "434"
        ["chemID"]=>
        string(3) "658"
        ["attribute"]=>
        string(5) "Clear"
      }
      [2]=>
      object(stdClass)#298 (9) {
        ["product_number"]=>
        string(9) "14004030C"
        ["size"]=>
        string(6) "30 Gal"
        ["catID"]=>
        string(1) "2"
        ["list_price"]=>
        string(6) "799.00"
        ["chemID"]=>
        string(3) "658"
        ["attribute"]=>
        string(5) "Clear"
      }
    }
  }
}


To do this, you'll need to loop through both objects and count the matches.
$cnt = 0;
foreach($products as $pkey=>$pobj) {
    foreach($carts as $ckey=>$cobj) {
        if($cobj->chemID == $pobj->barrels->chemID && $cobj->catID == $pobj->barrels->catID) {
          $cnt++;
        }
    }
}
echo 'Count of matches: ' . $cnt;

Here is a PHP Fiddle demo.

0 comments:

Post a Comment