结合在PHP多维数组值 [英] Combining Multidimensional Array Values in PHP

查看:114
本文介绍了结合在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:


  1. 添加航运量的值,其中的说明是相同的不同维度

  2. 挂断了阵列如果包含说明并没有在所有其他方面存在

  3. 挂断维度一旦航运量的组合

  1. Add the values of the shipping-amount where the description is the same across dimensions
  2. Drop the array if it contains a description which doesn't exist in every other dimension
  3. Drop a dimension once the shipping-amounts are combined

有可能有几个第一级阵列(不只是2如这里示出),但是这是一样深的尺寸将去。我在寻找以下结果:

There may be several first-level arrays (not just 2 as shown here), but this is as deep as the dimensions will go. 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
        )
) 

在此先感谢!

推荐答案

我认为这将工作:

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

我一直停留在某个问题上,在过去2天,感觉你,所以我希望这有助于。

I have been stuck on an issue for the past 2 days and feel you, so I hope this helps.

这篇关于结合在PHP多维数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆