1

The following works...

$a = ['a','b','c'=>[1,2,3]];
$b = &$a;
$b = &$b['c'];

So... I want to change the reference of an array with this function:

function change_array_pointer ( &$array, $path ) {    
    foreach ($path as $subpath) {
        $array = &$array[$subpath];
    }    
}

Looks simple, but doesn't work.

For example, this code is not working and I don't know why:

$a = ['a','b','c'=>[1,2,3]];
$b = &$a;
change_array_pointer($b,['c']);

In my opinion $b should be [1,2,3], but it's not.

Please explain to me what's happening :(

1 Answer 1

1
$array = &$array[$subpath];

References in php are not like C pointers. Change that with

$array = $array[$subpath];

What References Are

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

Comments

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.