1
    $menus = [
        0 => [
            'id' => 'home',
            'title' => 'Home',
            'url' => '/display/home',
            'children' => [],
            'parent' => null
        ],
        1 => [
            'id' => 'nodes',
            'title' => 'Nodes',
            'url' => 'nodes/index',
            'children' => [
                0 => [
                    'id' => 'addNode',
                    'title' => 'Add Node',
                    'url' => '/nodes/add',
                    'children' => [],
                    'parent' => "nodes"
                ],
                1 => [
                    'id' => 'editNode',
                    'title' => 'Edit Node',
                    'url' => '/nodes/edit',
                    'children' => [],
                    'parent' => 'nodes'
                ],
                2 => [
                    'id' => 'deleteNode',
                    'title' => 'Delete Node',
                    'url' => '/nodes/delete',
                    'children' => [
                        0 => [
                            'id' => 'deleteMultipleNodes',
                            'title' => 'Delete Multiple Nodes',
                            'url' => '/nodes/deleteall',
                            'children' => [
                                0 => [
                                    'id' => 'deleteMultipleSelectedNodes',
                                    'title' => 'Delete Multiple Selected Nodes',
                                    'url' => '/nodes/deleteallselected',
                                    'children' => [],
                                    'parent' => 'deleteMultipleNodes'
                                ]
                            ],
                            'parent' => 'deleteNode'
                        ]
                    ],
                    'parent' => 'nodes'
                ]

            ],
            'parent' => null
        ]
    ];

Assuming I have this array. What i want is to recursively search this array for an "id" and if found push a new children to the children array of that element.

I've tried it via different ways, I've also tried to use RecursiveArrayIterator to traverse the array, but the problem is how can i push value to that index of the array when found while traversing.

For Example here is a code from one of my tries:

private function traverseArray($array)
{

    $child = [
        'id' => 'deleteMultipleNotSelectedNodes',
        'title' => 'Delete Multiple Not Selected Nodes',
        'url' => '/nodes/deletenotselected',
        'children' => [],
        'parent' => 'deleteMultipleNodes'
    ];
    foreach($array as $key=>$value)
    {
        if(is_array($value))
        {
            $this->traverseArray($value);
        }
        if($key == "id" && $value == "deleteMultipleNodes") 
        {
            array_push($array["children"], $child); // This part is confusing me, How to add the child on this index where the id is found. 
        }
    }
}

Any help on how to do such thing in an efficient way would save my days.

14
  • If you fixed deleteMultipleNodes in your search then you must know the index, so what confusing? Commented May 12, 2016 at 13:09
  • push this child on what index? Commented May 12, 2016 at 13:10
  • Its recursively drilling down and if it founds the ID I want to to push the child to that id's element. But i don't have the index Commented May 12, 2016 at 13:11
  • There is a children, do you want one more similar to that? Commented May 12, 2016 at 13:11
  • 1
    Just an argument pass by value / reference issue. Commented May 12, 2016 at 13:28

1 Answer 1

4

Here it's how it would work without using $this and fixing bugs in comparing $value instead assigning anything to value.

Please note the difference with &$array and &$value, which are references, so it would change the original data instead of copying it into new variables.

<?php
$menus = [
        0 => [
            'id' => 'home',
            'title' => 'Home',
            'url' => '/display/home',
            'children' => [],
            'parent' => null
        ],
        1 => [
            'id' => 'nodes',
            'title' => 'Nodes',
            'url' => 'nodes/index',
            'children' => [
                0 => [
                    'id' => 'addNode',
                    'title' => 'Add Node',
                    'url' => '/nodes/add',
                    'children' => [],
                    'parent' => "nodes"
                ],
                1 => [
                    'id' => 'editNode',
                    'title' => 'Edit Node',
                    'url' => '/nodes/edit',
                    'children' => [],
                    'parent' => 'nodes'
                ],
                2 => [
                    'id' => 'deleteNode',
                    'title' => 'Delete Node',
                    'url' => '/nodes/delete',
                    'children' => [
                        0 => [
                            'id' => 'deleteMultipleNodes',
                            'title' => 'Delete Multiple Nodes',
                            'url' => '/nodes/deleteall',
                            'children' => [
                                0 => [
                                    'id' => 'deleteMultipleSelectedNodes',
                                    'title' => 'Delete Multiple Selected Nodes',
                                    'url' => '/nodes/deleteallselected',
                                    'children' => [],
                                    'parent' => 'deleteMultipleNodes'
                                ]
                            ],
                            'parent' => 'deleteNode'
                        ]
                    ],
                    'parent' => 'nodes'
                ]

            ],
            'parent' => null
        ]
    ];

function traverseArray(&$array)
{
    $child = [
        'id' => 'deleteMultipleNotSelectedNodes',
        'title' => 'Delete Multiple Not Selected Nodes',
        'url' => '/nodes/deletenotselected',
        'children' => [],
        'parent' => 'deleteMultipleNodes'
    ];
    foreach($array as $key=>&$value)
    {
        if(is_array($value))
        {
            traverseArray($value);
        }
        if($key == "id" && $value == "deleteMultipleNodes") 
        {
            array_push($array["children"], $child); 
        }
    }
}

echo "=== before \n";
var_export($menus);
echo "\n\n";
traverseArray($menus);
echo "=== after \n";
var_export($menus);
Sign up to request clarification or add additional context in comments.

5 Comments

Wow That worked. Can you please explain what &$array would do actually.
using array_walk_recursive is more efficient?
Pass by reference was a new concept to me in PHP and it worked like a dream. Thank you so much.
At least reference my comment above :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.