dongou3286 2016-10-06 11:49
浏览 100
已采纳

如何在PHP中合并此数组的重复元素?

I have an array like this:

$array = array(
    0 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "memory"),
    1 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "cpu"),
    2 => array("ordernumber" => "1", "name" => "John", "product" => "desktop", "component" => "cpu"),
    3 => array("ordernumber" => "2", "name" => "Pete", "product" => "monitor", "component" => "")
);

It contains data from different orders, but as you can see an order can contain multiple purchased products, and each product can contain different 'components'. There's alot of duplicate data in this array, so I would like to turn it into this:

$array = array(
    0 => array(
        "order" => array(
            "ordernumber" => "1", "name" => "John"
        ),
        "products" => array(
            0 => array(
                "name" => "laptop",
                "components" => array("memory", "cpu")
            ),
            1 => array(
                "name" => "desktop",
                "components" => array("cpu")
            )
        )
    ),
    1 => array(
        "order" => array(
            "ordernumber" => "2", "name" => "Pete"
        ),
        "products" => array(
            0 => array(
                "name" => "monitor",
                "components" => array()
            )
        )
    )
);

What would be a good way to do this?

  • 写回答

1条回答 默认 最新

  • dongzha0149 2016-10-06 12:21
    关注

    Please use below code to make the solution what you want

    <?php 
    
    $array = array(
        0 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "memory"),
        1 => array("ordernumber" => "1", "name" => "John", "product" => "laptop", "component" => "cpu"),
        2 => array("ordernumber" => "1", "name" => "John", "product" => "desktop", "component" => "cpu"),
        3 => array("ordernumber" => "2", "name" => "Pete", "product" => "monitor", "component" => "")
    );
    
    
    
    $final_array = [];
    foreach($array as $k=>$v){
        $final_array[$v['ordernumber']]['order']['ordernumber'] = $v['ordernumber'];
        $final_array[$v['ordernumber']]['order']['name'] = $v['name'];
    
        $final_array[$v['ordernumber']]['products'][$v['product']]['name'] = $v['product'];
        $final_array[$v['ordernumber']]['products'][$v['product']]['components'][] = $v['component'];
    }
    
    // You can skip this foreach if there will not metter of KEY of an array in your code!
    $final_array = array_values($final_array);
    foreach($final_array as $k=>$v){
        $final_array[$k]['products'] = array_values($final_array[$k]['products']);  
    }
    
    
    echo "<pre>";
    print_r($final_array);
    
    ?>
    

    its should work!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键