rahul singh
rahul singh

Reputation: 449

Update multidimensional array value depending on other sibling value

I have array this can be up to n element.

    (
[children] => Array
    (
        [0] => Array
            (
                [id] => nhbs0123620cf897
                [title] => Introduction
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => bylff0a76617c8
                                [title] => Courent3
                                [children] => Array
                                    (
                                    )
                            )

                        [1] => Array
                            (
                                [id] => xavs26efa2f51eb
                                [title] => Chapter
                                [children] => Array
                                    (
                                    )
                            )

                        [2] => Array
                            (
                                [id] => iezkd241d9d90
                                [title] => external
                                [children] => Array
                                    (
                                    )
                            )

                        [3] => Array
                            (
                                [id] => gmzh439c4f50
                                [title] => audio
                                [children] => Array
                                    (
                                    )
                            )

                        [4] => Array
                            (
                                [id] => niugd4e18b0
                                [title] => url
                                [children] => Array
                                    (
                                    )
                            )

                        [5] => Array
                            (
                                [id] => unpgdb1b7694
                                [title] => new
                                [children] => Array
                                    (
                                    )

                            )

                        [6] => Array
                            (
                                [id] => ssvc2025c0c8a
                                [title] => simple
                                [children] => Array
                                    (
                                    [0] => Array
                                        (
                                            [id] => ssvc2025c0c
                                            [title] => later
                                            [children] => Array
                                                (
                                                )
                                        )
                                    )
                            )

                    )

            )

        [1] => Array
            (
                [id] => rwtae5d9482
                [title] => Summary
                [children] => Array
                    (
                    [0] => Array
                            (
                                [id] => ssvc2025c0
                                [title] => later
                                [children] => Array
                                    (
                                    )
                            )
                    )

            )
        [2] => Array
            (
                [id] => rwtae5d9482709
                [title] => Course
                [children] => Array
                    (
                    )

            )

    )

  )

Here I want to update title value depending on id for each element in array.

What i have tried

1 used array_walk_recursive here i can update data but while updating not able to check id value .

 array_walk_recursive(array, function(&$value, $key) {
      // not able to check if id == 'something' .this is what i need 
        if ($key == 'title') {
            $value = 'updated data';
        }
    });

Second tried in for each loop but not able to hold array index to get actual array

 function myfun($array,&$out){
    foreach($array['children'] as $key=>$val){
        if(!empty($val['children'])){
            $out['children'][$key]['title'] = $val['title']; // this is not right key 
            $this->myfun($val,$out['children']);
        }else{
            $out['children'][$key]['title'] = $val['title'];  // this is not right key
        }
    }
}

here also not able to return array in $out array. Only thing i have that children key using this if any function can be written.

I am hoping this should be possible in php array functions.

Upvotes: 0

Views: 181

Answers (1)

dWinder
dWinder

Reputation: 11642

You are close - all you need is to use the & - this will send the array as reference so any change on him will be on the original array. Can look in PHP manual for more knowledge.

Consider the following:

$arr = [["id" => 1, "title" => "aaa", "children" => []], ["id" => 2, "title" => "bbb", "children" => [["id" => 3, "title" => "ccc", "children" => []]]]];

function modifyTitle(&$arr, $id, $newVal) {
    foreach($arr as &$e) {
        if ($e["id"] == $id)
            $e["title"] = $newVal;
        modifyTitle($e["children"], $id, $newVal);
    }

}
modifyTitle($arr, 3, "zzz");
print_r($arr); // now the original array children changed

Live example: https://3v4l.org/ZbiB9

Upvotes: 1

Related Questions