[英]Combining Multidimensional Array Values in PHP
I have a multidimensional array which looks like this: 我有一个多维数组,如下所示:
Array
(
[0] => Array
(
[0] => Array
(
[description] => UPS Ground
[delivery-time] => 1-5 business days
[shipping-amount] => 1299
)
[1] => Array
(
[description] => UPS 3 Day Select
[delivery-time] => 3 business days
[shipping-amount] => 2459
)
[2] => Array
(
[description] => UPS 2nd Day Air
[delivery-time] => 2 business days
[shipping-amount] => 3239
)
)
[1] => Array
(
[0] => Array
(
[description] => UPS Ground
[delivery-time] => 1-5 business days
[shipping-amount] => 864
)
[1] => Array
(
[description] => UPS 3 Day Select
[delivery-time] => 3 business days
[shipping-amount] => 1109
)
[2] => Array
(
[description] => UPS 2nd Day Air
[delivery-time] => 2 business days
[shipping-amount] => 1633
)
[3] => Array
(
[description] => UPS Overnight
[delivery-time] => 1 business day
[shipping-amount] => 3528
)
)
)
I'm trying to achieve 3 things: 我正在努力实现三件事:
shipping-amount
where the description
is the same across dimensions description
相同的shipping-amount
array
if it contains a description
which doesn't exist in every other dimension array
包含每个其他维度中不存在的description
,则删除该array
There may be several first-level arrays (not just 2 as shown here), but this is as deep as the dimensions will go. 可能有几个第一级数组(这里不仅仅是2个数组),但这与维度一样深。 I'm looking for the following result:
我正在寻找以下结果:
Array
(
[0] => Array
(
[description] => UPS Ground
[delivery-time] => 1-5 business days
[shipping-amount] => 2163
)
[1] => Array
(
[description] => UPS 3 Day Select
[delivery-time] => 3 business days
[shipping-amount] => 3568
)
[2] => Array
(
[description] => UPS 2nd Day Air
[delivery-time] => 2 business days
[shipping-amount] => 4872
)
)
Thanks in advance! 提前致谢!
I think this would work: 我认为这会奏效:
$final=array(); // the final array
$count=array(); // keeps track of instances of each description
$loops=count($array);
for($a=0;$a<$loops;$a++){
foreach($array[$a] as $s){ //loop through child arrays
if($count[$s['description']]>0){ //check if description exists in $count
foreach($final as $k=>$v){ //add sums to the final if it does exist
if($final[$k]['description']==$s['description']){$final[$k]['shipping-amount']+=$s['shipping-amount'];}
}
}else{ //if it doesn't exist in the count array, add it to the final array
$final[]=$s;
}
$count[$s['description']]++;//update the count array
}
}
//Unset singletons, using the count array
foreach($count as $k=>$v){
if($v==1){
foreach($final as $key=>$val){
if($final[$key]['description']==$k){unset($final[$key]);}
}
}
}
print_r($final);
I have been stuck on an issue for the past 2 days and feel you, so I hope this helps. 在过去的两天里,我一直被困在一个问题上并感受到你,所以我希望这会有所帮助。
I'm not going to write code because I agree with deceze . 我不会写代码,因为我同意deceze 。
Nonetheless, my recommendation would be a custom function that: 尽管如此,我的建议是一个自定义功能:
Given your specific requirements isn't a single, magical PHP function that does this. 鉴于您的具体要求不是一个单一,神奇的PHP功能,这样做。 Yet, there are dozens of PHP array functions you could utilize within your custom function.
但是,您可以在自定义函数中使用许多PHP数组函数。
If you need help on a particular piece, update your post or ask a new question. 如果您需要特定部分的帮助,请更新您的帖子或提出新问题。
我建议使用带有回调的array_walk函数,该回调可以处理您对添加和唯一性的特定要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.