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

时间:2015-11-10 21:22:42

标签: php arrays multidimensional-array

我有一个购物车,我使用两个阵列:一个用于从会话中获取购物车产品,另一个用于显示产品。我需要从购物车数组中获取计数值,从产品数组中获取相应的产品。我需要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"
      }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

为此,您需要遍历两个对象并计算匹配。

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

这是PHP Fiddle demo

答案 1 :(得分:0)

如果你想将cart_array中的count值写入procucts数组中的objetcs,你可以试试这个:

True