Wednesday, March 21, 2018

Arrange the array by pid - development

Arrange the array by pid

I have an array selected from database like this

$authList = [
    ['id' => 1, 'pid' => 0, 'title' => 'project'],
    ['id' => 2, 'pid' => 1, 'title' => 'customer'],
    ['id' => 3, 'pid' => 2, 'title' => 'select'],
];

And I want to arrange the array by “pid” like this

$result = [
    [
        'id' => 1,
        'pid' => 0,
        'title' => 'project',
        'children' => [
            [
                'id' => 2,
                'pid' => 1,
                'title' => 'select',
                'children' => [
                    'id' => 3,
                    'pid' => 2,
                    'title' => 'select'
                ]
            ],
        ]
    ],
];

So could anyone provide me an efficient function to arrive the goal. Any
response or suggestion would appreciated



from Laravel Questions and Answers https://laravelquestions.com/php/arrange-the-array-by-pid/
via Lzo Media

No comments:

Post a Comment