Jump to content

Recommended Posts

Lets say that we have several nested arrays, like in this example:

 

$some_array = array(
	'key_foo' => 'var_foo',
	'key_bar' => array(
		'_key_baz' => '_smth'
		'_key_other_baz' => array(
			'__key_some_foo' => '_foo_2',
			'__key_some_other_foo' => '_foo_3',
			'__key_some_other_foo' => '_quix'
		)
	)
	'key_quux' => 'var_foo_3',
	'key_baz' => array(
		'_key_smt' => '_quix_2'
	)
);

Let's also say that we have a reference:

 

$ref = &$some_array['key_bar']['_key_baz'];
Is it possible to, using that reference and that array, to find out what siblings, parents and children '_smth' has? I want some functionality like this:

get_parent($ref, $some_array) // returns a reference to 'key_bar'
get_next_sibling($ref, $some_array) // returns a reference to '_key_other_baz'
You get the idea. Does php have some out of the box functionality like that or it has to be manually implemented via recursively searching for reference?

 

Thanks for reading.

Edited by Goat
Link to comment
https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/
Share on other sites

It is not. You cannot start from $ref and derive anything in $some_array - you only have a reference to the string "_smth", not to a location in an array.

Thanks for answering, but are you sure about that? I don't know how exactly references look like in the memory, but if reference contains a pointer to '__smth' then it should be possible to recursively find value with the same pointer in the array. In fact I can create such function myself if someone can provide a syntax for checking if a reference and a variable point to the same value.

Edited by Goat

It contains a "pointer" (only kinda) to the string value itself. The string doesn't know that it's contained in an array or whatever, so the variable with the reference doesn't know that either.

 

Now, maybe I misread but I thought you were asking about just having the $ref and figuring out more information from there. That's not possible. If you have both $ref and the source array then it's a different question.

But only kinda possible. The language doesn't expose a way to tell if two variables both reference the same value. You can only tell if a variable is a reference, and even then it's very hacky and I wouldn't dare suggest it to anyone to actually use. As a workaround you could loop through everything, modify $ref to a random value, and check if the source array's item is also modified (or vice versa) but that's crazy.

 

Please think of another way to do this. If you want a location in an array then use a set of keys, like as used in breadcrumbs. References are not suitable for it.

Thanks for your answer.

 

It contains a "pointer" (only kinda) to the string value itself. The string doesn't know that it's contained in an array or whatever, so the variable with the reference doesn't know that either.

I was aware of all that.

 

Now, maybe I misread but I thought you were asking about just having the $ref and figuring out more information from there. That's not possible. If you have both $ref and the source array then it's a different question.

I agree. That's why my example says get_parent($ref, $some_array). I knew that reference alone just knows about the string, not the places where that string might be.

 

But only kinda possible. The language doesn't expose a way to tell if two variables both reference the same value. You can only tell if a variable is a reference, and even then it's very hacky and I wouldn't dare suggest it to anyone to actually use. As a workaround you could loop through everything, modify $ref to a random value, and check if the source array's item is also modified (or vice versa) but that's crazy.

Well that's a bummer. I really expected PHP to have some way of comparing two references to see if they contain the same value. It kinda sucks that it doesn't.

 

Please think of another way to do this. If you want a location in an array then use a set of keys, like as used in breadcrumbs. References are not suitable for it.

Yeah some sort of breadcrumb secondary array seems like the only hope then. Maybe I should create a class called browseable_array or something that has main array, breadcrumb array and parent/sibling/child methods in it? Has anyone else done something similar? Is there some other nested datatype in PHP that I can use? Maybe convert array to an XML object, assuming PHP XML api has the functionality I need (I never tried using XML in php)?

 

Thanks for taking interest.

Edited by Goat
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.