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.

1
  • 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? 3v4l.org/rmXQ2 Commented Aug 1, 2017 at 19:12

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

6 Comments

@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.
I had tried passing as reference to the loop, but not the function. Let me give it a try.
when I add "&" to the function parameter, I get "Fatal error: Cannot pass parameter 1 by reference"
the parameter passed to the function needs to be a variable smthng like `change_recursively(['x'=>'y']); will not work
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.
|

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.