繁体   English   中英

如果发现值,如何比较两个多维数组并将可变性加到一个数组上?

[英]How to compare two multidimensional array and add variabile to one if values found?

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

chemID和catID值使产品唯一。

我不想更改数组结构,只想将计数值添加到产品数组中。...请帮助

购物车阵列:

 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;

这是一个PHP Fiddle演示

如果您希望将cart_array中的计数值写入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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM