nineday900 2019-08-13 19:17 采纳率: 57.1%
浏览 476
已采纳

二维数组根据相同值组成新数组?

旧数组:

$orders=array(
array(
"ordersn"=>'2019564842348',
"proName"=>'拖鞋',
"name"=>'张三',
"phone"=>"1800952442",
"proNum"=>2,
"proPrice"=>35.85
),
array(
"ordersn"=>'2019564842348',
"proName"=>'棉签',
"name"=>'张三',
"phone"=>"1800952442",
"proNum"=>1,
"proPrice"=>12
),
array(
"ordersn"=>'25451548412487',
"proName"=>'电脑',
"name"=>'李四',
"phone"=>"1564845487",
"proNum"=>1,
"proPrice"=>4600
),
);

如何通过相同的ordersn组装成新数组:

$orders=array(
    array(
            "ordersn"=>'2019564842348',
            "product"=>array(
                    array(
                            "proName"=>'拖鞋',
                            "proNum"=>2,
                            "proPrice"=>35.85
                        ),
                    array(
                            "proName"=>'棉签',
                            "proNum"=>1,
                            "proPrice"=>12
                        ),
                ),
            "name"=>'张三',
            "phone"=>'1800952442'
        ),
    array(
            "ordersn"=>'25451548412487',
            "product"=>array(
                    array(
                            "proName"=>'电脑',
                            "proNum"=>1,
                            "proPrice"=>4600
                        ),
                ),
            "name"=>'李四',
            "phpone"=>"1564845487",
        ),
    );
  • 写回答

1条回答 默认 最新

  • weixin_43867346 2019-08-14 11:30
    关注
    $orders=array(
    array(
    "ordersn"=>'2019564842348',
    "proName"=>'拖鞋',
    "name"=>'张三',
    "phone"=>"1800952442",
    "proNum"=>2,
    "proPrice"=>35.85
    ),
    array(
    "ordersn"=>'2019564842348',
    "proName"=>'棉签',
    "name"=>'张三',
    "phone"=>"1800952442",
    "proNum"=>1,
    "proPrice"=>12
    ),
    array(
    "ordersn"=>'25451548412487',
    "proName"=>'电脑',
    "name"=>'李四',
    "phone"=>"1564845487",
    "proNum"=>1,
    "proPrice"=>4600
    ),
    );
    $returnData = [];
    foreach($orders as $k => $v){
    if(isset($returnData[$v['ordersn']])){
            $returnData[$v['ordersn']]['product'][] = [
                    'proName'  => $v['proName'],
                    'proNum'   => $v['proNum'],
                    'proPrice' => $v['proPrice'] 
            ];
        }else{
            $returnData[$v['ordersn']] = [
                'ordersn' => $v['ordersn'],
                'product' => [[
                    'proName'  => $v['proName'],
                    'proNum'   => $v['proNum'],
                    'proPrice' => $v['proPrice'] 
                ]],
                'name'  => $v['name'],
                'phone' => $v['phone']
            ];
        }
    }
    print_r(array_values($returnData));die;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ChatGPT网络被篡改怎么办?
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。
  • ¥20 在easyX库下编写C语言扑克游戏跑的快,能实现简单的人机对战
  • ¥15 svpwm波形异常求解答
  • ¥15 STM32——硬件IIC从机通信代码实现
  • ¥15 微生物组数据分析--微生物代谢物
  • ¥30 求一跃动小子保卫主公Java算法实现