Adam Posted March 11, 2010 Share Posted March 11, 2010 Hey everyone! Bit stuck here. Not sure if I'm thinking about this too much or whether it's just insanely hard. Basically I want to return a value from a multidimensional array based off of a string with delimiters. Probably easier to show you than to explain: $str = 'str1.str2.str3'; $array = array( 'str1' => array( 'str2' => array( 'str3' => 'I want to return this...', ), ), ); So using $str I want to traverse through the array to return 'I want to return this...' Any body have any ideas? I'm pretty stumped! Thanks in advance, Adam Quote Link to comment https://forums.phpfreaks.com/topic/194912-select-value-from-a-multidimensional-array-using-a-delimited-string/ Share on other sites More sharing options...
Catfish Posted March 11, 2010 Share Posted March 11, 2010 1. explode $str on period character . 2. use each str as a key reference from the array: print($array[$str1][$str2][$str3]); Quote Link to comment https://forums.phpfreaks.com/topic/194912-select-value-from-a-multidimensional-array-using-a-delimited-string/#findComment-1024878 Share on other sites More sharing options...
Adam Posted March 11, 2010 Author Share Posted March 11, 2010 Sorry. I should have mentioned I don't know how many levels the string will go into, should be unlimited. Quote Link to comment https://forums.phpfreaks.com/topic/194912-select-value-from-a-multidimensional-array-using-a-delimited-string/#findComment-1024923 Share on other sites More sharing options...
DavidAM Posted March 11, 2010 Share Posted March 11, 2010 something along these lines should work. You just keep capturing a pointer to the element of interest until you have traversed the entire list of keys. $str = 'str1.str2.str3'; $array = array( 'str1' => array( 'str2' => array( 'str3' => 'I want to return this...', ), ), ); $keys = explode ($str, "."); // or is it explode(".", $str)? I can never remember $hold = &$array; foreach ($keys as $key) { $hold = &$hold[$key]; } // $hold now points to the field you are looking for Watchout though. If $str starts out as "str1.str2" you will end up with $hold referencing $array[str1][str2] which is an array NOT a string value. Quote Link to comment https://forums.phpfreaks.com/topic/194912-select-value-from-a-multidimensional-array-using-a-delimited-string/#findComment-1024981 Share on other sites More sharing options...
Adam Posted March 12, 2010 Author Share Posted March 12, 2010 Thanks for that DavidAM. I actually managed to solve it in a slightly different way using a recursive function: public function get($property) { $tokens = explode('.', $property); $return = $this->properties; foreach ($tokens as $token) { $return = $this->recurseGet($token, $return); if (!$return) { break; } } return $return; } protected function recurseGet($token, $properties) { if (array_key_exists($token, $properties)) { return $properties[$token]; } return false; } Thanks anyway though, much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/194912-select-value-from-a-multidimensional-array-using-a-delimited-string/#findComment-1025112 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.