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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,