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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器