M
M
Mikha Pankratov2017-12-16 14:22:20
PHP
Mikha Pankratov, 2017-12-16 14:22:20

Is there a better way to iterate over an array?

Good afternoon, I need to collect an array of data like this

$test = [
            ['title' => 'test1', 'url' => 'test1'],
            ['title' => 'test2', 'url' => 'test2'],
            [
                'title' => 'test3',
                'url' => 'test3',
                'children' => [
                    [
                        'title' => 'test4',
                        'url' => 'test3/test4',
                        'children' => [
                            [
                                'title' => 'test5',
                                'url' => 'test3/test4/test5',
                            ]
                        ]
                    ],
                ]
            ],
            ['title' => 'test6', 'url' => 'test6'],
        ];

The initial data is
$data = [
    [
        'id' => 1,
        'parent_id' => 0,
        'name' => 'test1',
        'url' => 'test1',
    ],
    [
        'id' => 2,
        'parent_id' => 0,
        'name' => 'test2',
        'url' => 'test2',
    ],
    [
        'id' => 3,
        'parent_id' => 0,
        'name' => 'test3',
        'url' => 'test3',
    ],
    [
        'id' => 4,
        'parent_id' => 3,
        'name' => 'test4',
        'url' => 'test3/test4',
    ],
    [
        'id' => 5,
        'parent_id' => 4,
        'name' => 'test5',
        'url' => 'test3/test4/test5',
    ],
    [
        'id' => 6,
        'parent_id' => 0,
        'name' => 'test6',
        'url' => 'test6',
    ],
];

I collect an array that I almost need with 2 functions
public function recarray($arr, $data) {
        foreach ($arr as $k => $val) {
            if($k === (int)$data['parent_id']){
                $arr[$k]['children'][(int)$data['id']] = [
                            'title' => $data['name'],
                            'url' => $data['url']
                ];
            }elseif (is_array($arr[$k])){
                $ret = $this->recarray($arr[$k], $data);
                if (count($ret)) $arr[$k] = $ret;
            }
        }
        return $arr;
    }

    public function getTestArr(){
        $tree = [];
        foreach ($data as $k=>$v) {
            if(@$v['parent_id']){
                $tree = $this->recarray($tree, $v);
            }else{
                $tree[$v['id']] = [
                    'title' => $v['name'],
                    'url' => $v['url'],
                ];
            }
        }
        return $tree;
    }

With the help of my method, the array is collected, but in a slightly different form, the keys are arranged according to id - but I need them to go sequentially from 0 .... n as in the example above, what are your thoughts?
this is the result i get
Array
(
    [1] => Array
        (
            [title] => test1
            [url] => test1
        )

    [2] => Array
        (
            [title] => test2
            [url] => test2
        )

    [3] => Array
        (
            [title] => test3
            [url] => test3
            [children] => Array
                (
                    [4] => Array
                        (
                            [title] => test4
                            [url] => test3/test4
                            [children] => Array
                                (
                                    [5] => Array
                                        (
                                            [title] => test5
                                            [url] => test3/test4/test5
                                        )

                                )

                        )

                )

        )

    [6] => Array
        (
            [title] => test6
            [url] => test6
        )

)

Of course, you can solve the problem with the help of another enumeration of the resulting array !!! But that's not the point - it's about avoiding it and finding the best way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2017-12-16
@frmax

Instead of your line $tree[$v['id']] = [...use$tree[] = [...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question