dongweizhen2009 2017-07-01 20:01
浏览 40
已采纳

嵌套的多维数组到单个多维数组

I have found numerous examples to convert a multidimensional array to a single dimensional array. I am needing to keep the data in the arrays together, just bring them out of nesting and into the first main array.

Here is the array:

Array
(
    [0] => Array
        (
            [id] => 2
            [username] => user2
            [downline] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [username] => user5
                            [downline] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 9
                                            [username] => user9
                                            [downline] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 15
                                                            [username] => user15
                                                        )
                                                    [1] => Array
                                                        (
                                                            [id] => 16
                                                            [username] => user16
                                                        )
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [id] => 10
                                            [username] => user10
                                            [downline] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 17
                                                            [username] => user17
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => Array
        (
            [id] => 3
            [username] => user3
            [downline] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [username] => user6
                            [downline] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 11
                                            [username] => user11
                                            [downline] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 18
                                                            [username] => user18
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
    [2] => Array
        (
            [id] => 4
            [username] => user4
            [downline] => Array
                (
                    [0] => Array
                        (
                            [id] => 7
                            [username] => user7
                            [downline] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 12
                                            [username] => user12
                                            [downline] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 19
                                                            [username] => user19
                                                        )
                                                )
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 8
                            [username] => user8
                            [downline] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 13
                                            [username] => user13
                                        )
                                    [1] => Array
                                        (
                                            [id] => 14
                                            [username] => user14
                                        )
                                )
                        )
                )
        )
)

This is for messaging all their team members. I don't care who they are under, I just need a full running list of all the users, so I can loop through and send them a message in the system.

So here is an example of the final array I am trying to get:

[
    [id] => 2
    [username] => user2
],
[
    [id] => 5
    [username] => user5
],
[
    [id] => 9
    [username] => user9
],
[
    [id] => 15
    [username] => user15
],
[
    [id] => 16
    [username] => user16
],
[
    [id] => 10
    [username] => user10
],
[
    [id] => 17
    [username] => user17
],
[
    [id] => 3
    [username] => user3]
],
// ... and so on

Ideally, the array keys could be their user id. This will be going in a dropdown menu where they can choose a member of their team, or they can message all of them. Something like this:

[
    1 => 'user1',
    2 => 'user2',
    9 => 'user9',
]

I have tried variations of array_merge, array_walk_recursive.. Everything just flattens it all into one ugly 1 dimensional array..

Something like this, which didn't work:

function flatten(array $arr) {
    return array_reduce($arr, function ($c, $a) {
        return is_array($a) ? array_merge($c, flatten($a)) : array_merge($c, [$a]);
    },
    []);
}
  • 写回答

1条回答 默认 最新

  • dotwc62080 2017-07-01 20:15
    关注

    The solution using array_walk_recursive function (to gather consequtive id/username pairs):

    // $arr is your initial array
    $result = [];
    array_walk_recursive($arr, function($v, $k) use (&$result){  
        if ($k == 'id') $result[] = ['id'=> $v];
        if ($k == 'username') {
            $result[count($result)-1]['username'] = $v;
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?