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 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件