PHP: can I determine the name of the parent array and the key from the link to the array element?

suppose we have such an array

$arr=array(array('a'=>1,'b'=>2),array('c'=>3,'d'=>4));

and a link to one of its elements

$element=&$arr[1]['c'];

My question is, is it possible to return to the original array using only the link? That is, returning to the parent array in some way without knowing it by name ... This would be useful for me in a more complex scenario.

+5
source share
3 answers

No, this, of course, is impossible. Being a β€œlink” (as PHP calls it, it’s actually a copy inhibitor), there’s no help. You will need to save the original array along with the element.

$elArrPair = array(
    "container" => $arr,
    "element"   => &$arr[1]['c'],
);

$elArrPair["element"] = $newValue - .

+5

$element $arr. in_array(), , $element $arr.

0

You copy contect from one variable to another, nothing else, there is no connection between the two variables.

0
source

All Articles