2

Firs off, I am struggling to phrase the questions correctly... so bear with me. I am trying to modify the given array using a foreach loop that runs through each array key/value recursively. However, I do not want to replace a key or value, instead I need to add a new key and value to the same level as the matched key.

In the code below I walk through each key value in the array, and if a key and value match, I want to add a new key to that same level where the match occurred. I can find the match, but I don't know how to update the array. I would rather update the existing array than build a new one.

Here's what I am trying to accomplish

Given this array:

array(
    array(
        'name' => 'name',
        'label' => 'Label'
    ),
    array(
        'name' => 'name',
        'label' => 'Label',
        'sub_fields' => array(
            array(
                'name' => 'my_desired_target',
                'label' => 'Label'
           )
        )
    )
);

function change_recursively($arr){

    foreach($arr as $key => $val){

        if(is_array($val)){

            change_recursively($val);

        }else{

            if($key == 'name' && $val == 'my_desired_target'){

                //How to add new key here?
                $arr['new_key'] = 'my new value';

            }

        }

    }

}

Below is the result I am looking for if I were to var_dump $arr. In this case the key 'name' and the value 'my_desired_target" were matched, and a new key 'new_key' and value 'my new value' were added to that same level.

array(
    array(
        'name' => 'name',
        'label' => 'Label'
    ),
    array(
        'name' => 'name',
        'label' => 'Label',
        'sub_fields' => array(
            array(
                'name' => 'my_desired_target',
                'label' => 'Label',
                'new_key' => 'my new value'
           )
        )
    )
)

I have looked into using array_walk_recursive, and array_map, but neither function seems to track how deep you are in the array.

Andreas
  • 23,610
  • 6
  • 30
  • 62
gdaniel
  • 1,307
  • 2
  • 10
  • 17
  • 1
    I'm almost there but I don't know how to finnish it off. It's like a brick wall hit me right there on the finnish line. I have the path but how do I convert that to a array address? https://3v4l.org/rmXQ2 – Andreas Aug 01 '17 at 19:12

1 Answers1

1

Use references:

Helpful link: PHP foreach change original array values

Your working code:

$arr = array(
    array(
        'name' => 'name',
        'label' => 'Label'
    ),
    array(
        'name' => 'name',
        'label' => 'Label',
        'sub_fields' => array(
            array(
                'name' => 'my_desired_target',
                'label' => 'Label'
            )
        )
    )
);

function change_recursively(&$arr){

    foreach($arr as $key => &$val){

        if(is_array($val)){

            change_recursively($val);

        }else{

            if($key == 'name' && $val == 'my_desired_target'){

                //How to add new key here?
                $arr['new_key'] = 'my new value';

            }

        }
    }
}
change_recursively($arr);
print_r($arr);

Hope it helps!

Keloo
  • 1,368
  • 1
  • 17
  • 36
  • @gdaniel Did it work for you. Make sure you use the reference in the `foreach` loop as well. The idea is that by default the `foreach $value` is a copy of the original array $value. – Keloo Aug 01 '17 at 19:18
  • I had tried passing as reference to the loop, but not the function. Let me give it a try. – gdaniel Aug 01 '17 at 19:27
  • when I add "&" to the function parameter, I get "Fatal error: Cannot pass parameter 1 by reference" – gdaniel Aug 01 '17 at 19:32
  • the parameter passed to the function needs to be a variable smthng like `change_recursively(['x'=>'y']); will not work – Keloo Aug 01 '17 at 19:36
  • Yeah, I fixed that. Still working on it. Get back to you soon. – gdaniel Aug 01 '17 at 19:38
  • 1
    I will mark the answer as correct, as it works isolated from my code. Unfortunately the array I am looping through is a class property and although $arr is updating the class property is not, and they are the same. Thanks for looking into this. – gdaniel Aug 01 '17 at 19:51