Chris
Chris

Reputation: 417

Searching for a key in a multidimensional array then changing a value with PHP

I have a multidimensional array that looks like this

[0] => Array
    (
        [recordId] => 5
        [leaf] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [recordId] => 6
                        [leaf] => 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [recordId] => 7
                                        [leaf] => 1
                                    )
                            )
                    )
                [1] => Array
                    (
                        [recordId] => 8
                        [leaf] => 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [recordId] => 9
                                        [leaf] => 1
                                    )
                                [1] => Array
                                    (
                                        [recordId] => 10
                                        [leaf] => 1
                                    )
                            )
                    )
            )
    )

Each node has a 'leaf' key that is TRUE by default and has a 'children' array if there are further nodes down.

I need to set the 'leaf' key value to FALSE if there is a 'children' array contained in the node. That way only final nodes have the leaf = TRUE designation.

I've tried searching but can't find code to do what I need and I can't wrap my head around the recursive function that I believe is needed.

Any ideas how I could accomplish this in PHP?

Thanks for the help.

Upvotes: 3

Views: 709

Answers (3)

hakre
hakre

Reputation: 198217

Walking on the actual $array:

array_walk($array, $walker = function (&$node) use (&$walker) {
    $node['leaf'] = (int) empty($node['children'])
        OR array_walk($node['children'], $walker);
});

A bit cryptic maybe, so you gotta love PHP.

Upvotes: 3

Naftali
Naftali

Reputation: 146360

In theory this should work:

function findChild(&$array){
     foreach($array as &$arr){
            if(isset($arr['children'])){
                  $arr['leaf'] = 0; //there are children
                  findChild($arr['children']);
            }
            else {
                  $arr['leaf'] = 1; //there are no children
            }
     }
}

Here is a working demo: http://codepad.org/AnYiRpES

Upvotes: 3

netcoder
netcoder

Reputation: 67745

Pretty simple actually:

function leafOrNotLeaf(array $array) {
    foreach ($array as $key => $sub) {
        if (isset($sub['children'])) {
            $array[$key]['leaf'] = false;
            $array[$key]['children'] = leafOrNotLeaf($sub['children']);
        }
    }
    return $array;
}

$new_array = leafOrNotLeaf($array);

Upvotes: 3

Related Questions