Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果找到值,如何比较两个多维数组并向一个数组添加变量?_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 如果找到值,如何比较两个多维数组并向一个数组添加变量?

Php 如果找到值,如何比较两个多维数组并向一个数组添加变量?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个购物车,我使用两个数组:一个用于从会话中获取购物车产品,另一个用于显示产品。我需要从cart数组中获取计数值,从products数组中获取共同响应产品。我需要产品->桶数组中的计数值,因为我将使用占位符来显示现有值 chemID和catID值使产品独一无二 我不想更改数组结构,只想将计数值添加到products数组中…请帮助 购物车阵列: array(2) { [0]=> object(stdClass)#224 (9) { ["chemID"]=>

我有一个购物车,我使用两个数组:一个用于从会话中获取购物车产品,另一个用于显示产品。我需要从cart数组中获取计数值,从products数组中获取共同响应产品。我需要产品->桶数组中的计数值,因为我将使用占位符来显示现有值

chemID和catID值使产品独一无二

我不想更改数组结构,只想将计数值添加到products数组中…请帮助

购物车阵列:

 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"
  }
}
产品阵列:

 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"
      }
    }
  }
}

为此,您需要在两个对象之间循环并计算匹配项

$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;

以下是一个。

如果您希望将cart_数组中的计数值写入到procucts数组中的objetcs中,您可以尝试以下方法:

foreach($cart_array as $cart_value){
        foreach($products_array[$cart_value->chemID]]["barrels"] as $products_value){
            if($products_value->catID == $cart_value->catID){
                $products_value->count=$cart_value->count;
            }
        }
}

#Set count=0 in all barrels in products_array
foreach($products_array as $value1){
    foreach($value1["barrels"] as $value2){
        if(!isset($value2->count)){
            $value2->count = 0;
        }
    }
}