1

I have an array that looks something like this.

Array
(
    [consultant] => Array
        (
            [John Smith] => Array
                (
                    [General] => Array
                        (
                            [PCA] => 0
                            [NCA] => 0
                        )
                )
        )
)

How do I append a array to PCA in place of the 0

to look like this.

Array
(
    [consultant] => Array
        (
            [John Smith] => Array
                (
                    [General] => Array
                        (
                            [PCA] => Array
                                (
                                    [Motor Block] => 0
                                )
                            [NCA] => 0
                        )
                )
        )
)

http://sandbox.onlinephpfunctions.com/code/d9b20040517e557fe93fdf1208079a619dcc213b

1

1 Answer 1

3

Just find the proper path from the array structure and assign the array to this key:

$array['consultant']['John Smith']['General']['PCA'] = array('Motor Block'=>0);

If you however want to change the value 0 on key PCA to this array no matter how deep this value is, use array_walk_recursive:

function change(&$v,$k){
    if($v == 0 && $k == 'PCA'){
        $v = array('Motor Block'=>0);
    }
}
array_walk_recursive($array,'change');

http://sandbox.onlinephpfunctions.com/code/3af10b9c88dcbc1af474a743a6c4a7cf5687f3ba

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

3 Comments

I would think the OP already knows how to do that, no? I was thinking more of a recursive function that would loop till the res is not an array.
@Grimbode, Yes thats what I had in mind. Sorry if my question was not clear. Thanks Grimbode. I have updated the question to try and make it more understandable.
@Grimbode OK, thanks for remark, I updated my answer with working solution

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.