doutao1939 2017-01-17 14:05
浏览 30
已采纳

迭代数组并创建多维

For simplicity I have an array:

 Array (
[0] => Array
    (
        [content] => Item One
        [type] => Breakfast
    )
[1] => Array
    (
        [content] => Item Two
        [type] => Breakfast
    )
[2] => Array
    (
        [content] => Item One
        [type] => Lunch
    )
[3] => Array
    (
        [content] => Item One
        [type] => Dinner
    )
 )

What is the most efficient way to create a new multidimensional array where it would combine on matching key "type"? Like below. Current trying in a foreach. Are there any built in functions for this?

 Array (
[0] => Array
    (
        [content] => Item One
        [type] => Breakfast
    ),
    (
        [content] => Item Two
        [type] => Breakfast
    )
[1] => Array
    (
        [content] => Item One
        [type] => Lunch
    ),
[2] => Array
    (
        [content] => Item One
        [type] => Dinner
    )
 )
  • 写回答

2条回答 默认 最新

  • drnrxv9383 2017-01-17 14:12
    关注

    You have to iterate through the input array and create a new output array. Take a look at this simple example:

    <?php
    $input = [
        [
            'content' => 'Item One',
            'type' => 'Breakfast',
        ],
        [
            'content' => 'Item Two',
            'type' => 'Breakfast',
        ],
        [
            'content' => 'Item Three',
            'type' => 'Lunch',
        ],
        [
            'content' => 'Item Four',
            'type' => 'Dinner',
        ]
    ];
    $output = [];
    
    array_walk(
        $input,
        function($element) use (&$output) {
            $output[$element['type']][] = $element;
        }
    );
    print_r($output);
    

    The output of above obviously is:

    Array
    (
        [Breakfast] => Array
            (
                [0] => Array
                    (
                        [content] => Item One
                        [type] => Breakfast
                    )
    
                [1] => Array
                    (
                        [content] => Item Two
                        [type] => Breakfast
                    )
    
            )
    
        [Lunch] => Array
            (
                [0] => Array
                    (
                        [content] => Item Three
                        [type] => Lunch
                    )
    
            )
    
        [Dinner] => Array
            (
                [0] => Array
                    (
                        [content] => Item Four
                        [type] => Dinner
                    )
    
            )
    
    )
    

    Of course you can place whatever element and structure you want inside the created array. For this example I just accepted the original elements, since that is irrelevant for demonstrating how to easily iterate through the array.

    If you insist on a numeric key sequence in the output array, then you can simply use print_r(array_values($output)); instead of print_r($output)...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示