Goat Posted May 30, 2013 Share Posted May 30, 2013 (edited) 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 May 30, 2013 by Goat Quote Link to comment https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/ Share on other sites More sharing options...
requinix Posted May 30, 2013 Share Posted May 30, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/#findComment-1433261 Share on other sites More sharing options...
Goat Posted May 30, 2013 Author Share Posted May 30, 2013 (edited) j Edited May 30, 2013 by Goat Quote Link to comment https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/#findComment-1433262 Share on other sites More sharing options...
requinix Posted May 30, 2013 Share Posted May 30, 2013 I hope that wasn't a bump. Quote Link to comment https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/#findComment-1433263 Share on other sites More sharing options...
Goat Posted May 30, 2013 Author Share Posted May 30, 2013 (edited) I hope that wasn't a bump. No, sorry, it was not, I was trying to edit post, but I hit quote accidentally, And I couldn't find delete post in this new interface. I hope admin can delete my post. Edited May 30, 2013 by Goat Quote Link to comment https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/#findComment-1433264 Share on other sites More sharing options...
Goat Posted May 30, 2013 Author Share Posted May 30, 2013 (edited) 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 May 30, 2013 by Goat Quote Link to comment https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/#findComment-1433265 Share on other sites More sharing options...
requinix Posted May 30, 2013 Share Posted May 30, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/#findComment-1433267 Share on other sites More sharing options...
Goat Posted May 30, 2013 Author Share Posted May 30, 2013 (edited) 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 May 30, 2013 by Goat Quote Link to comment https://forums.phpfreaks.com/topic/278612-browsing-nested-array-using-reference/#findComment-1433269 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.