[英]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.