Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中组合多维数组值_Php_Arrays_Multidimensional Array - Fatal编程技术网

在PHP中组合多维数组值

在PHP中组合多维数组值,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个多维数组,如下所示: Array ( [0] => Array ( [0] => Array ( [description] => UPS Ground [delivery-time] => 1-5 business days [shipping-amount] =&

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

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
                )

        )

)
我正在努力实现三件事:

  • 添加
    装运金额
    的值,其中
    说明
    在各维度上相同
  • 如果
    数组包含其他维度中不存在的
    说明
    ,请删除该数组
  • 合并装运金额后,删除维度
  • 可能有几个一级数组(不只是如图所示的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
            )
    ) 
    

    提前谢谢

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

    尽管如此,我的建议将是一个自定义函数:

    • 输入数组上的循环
    • 应用您概述的逻辑
    • 返回压缩数组
    鉴于您的特定需求,没有一个神奇的PHP函数可以做到这一点。然而,您可以在自定义函数中使用许多


    如果您需要某一特定内容的帮助,请更新您的帖子或提出新问题。

    我建议您使用带有回调的函数,该函数可处理您对添加和唯一性的特定要求。

    我认为这会起作用:

    $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);
    
    $final=array();//最终数组
    $count=array();//跟踪每个描述的实例
    $loops=计数($array);
    对于($a=0;$a0){//检查$count中是否存在描述
    foreach($k=>$v为final){//如果final确实存在,则向其添加总和
    如果($final[$k]['description']==$s['description']){$final[$k]['shipping-amount']+=$s['shipping-amount'];}
    }
    }else{//如果count数组中不存在,则将其添加到最终数组中
    $final[]=$s;
    }
    $count[$s['description']]++//更新计数数组
    }
    }
    //使用计数数组取消设置单例
    foreach($k=>v){
    如果($v==1){
    foreach($key=>$val的最终值){
    如果($final[$key]['description']==k){unset($final[$key]);}
    }
    }
    }
    打印(最终版);
    

    在过去的两天里,我一直被一个问题困扰着,感觉到你的存在,所以我希望这能有所帮助。

    这听起来太像“请为我编写代码”,而不是对某个特定问题的描述。你尝试了什么,你被困在哪里?我尝试了什么?我试着通过阅读PHP手册中的几十个非常荒谬的数组函数来找出适合这种情况的函数。我想它可能是一个
    foreach
    array\u merge
    array\u map
    key()
    的组合,但我不知道哪个顺序或函数最有意义。我以前在这里遇到过一些挑战我的问题,我得到了很大的帮助,所以我想我应该再问一次,而不是浪费时间到处闲逛。不过,浪费时间到处闲逛通常是学会自己动手的一个真正方法o) 如果它不起作用,至少你可以调整一下,得到你想要的结果。谢谢蒂姆。即使它不起作用,我也不想复制/粘贴。只是为了让我从正确的方向开始。为你干杯。