在PHP中组合多维数组值

时间:2012-03-02 23:40:20

标签: php arrays multidimensional-array

我有一个多维数组,如下所示:

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
                )

        )

)

我正在努力实现三件事:

  1. 添加shipping-amount的值description跨维度
  2. 如果array包含其他维度不存在的description,则将其删除
  3. 合并出货金额后删除维度
  4. 可能有几个第一级数组(不仅仅是这里显示的2个),但这和维度一样深。我正在寻找以下结果:

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

    提前致谢!

3 个答案:

答案 0 :(得分:2)

我认为这样可行:

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

过去两天我一直困扰着一个问题并感受到你,所以我希望这会有所帮助。

答案 1 :(得分:0)

我不打算编写代码,因为我同意 deceze

尽管如此,我的推荐是一个自定义功能:

  • 循环输入数组
  • 应用您概述的逻辑
  • 返回压缩数组

鉴于您的具体要求不是一个神奇的PHP函数,它可以做到这一点。然而,您可以在自定义函数中使用数十个PHP array functions

如果您需要有关某件作品的帮助,请更新您的帖子或提出新问题。

答案 2 :(得分:0)

我建议使用array_walk函数和回调来处理您对添加和唯一性的特定要求。