Jump to content

Select Value From A Multidimensional Array Using A Delimited String?


MrAdam
 Share

Recommended Posts

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

Link to comment
Share on other sites

Use explode to break the string up into an array of tokens, and then you'll need to loop through the main array looking for the element which matches the next token in the token array. So you'll need a counter to keep track of which token you're currently looking for. This will probably need to be a recursive function, unless the main array has a limit to how many dimensions it can have.

Link to comment
Share on other sites

Cheers pal. Knew it was along those lines just needed it straight in my head. Came up with this:

	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;	}

Link to comment
Share on other sites

  • 1 month later...

Hi again.Sorry it's been a while, but I've come back to this after a bit of a break and stuck trying to write the opposite set() part of it. Obviously it's completely different as you can't just build up recursively to the value. I just wondered if you had any thoughts or ideas as I've been stuck for countless hours on it.Thanks for you help,Adam

Link to comment
Share on other sites

What do you mean? You have a structure like this:

$array = array(	'str1' => array(		'str2' => array(			'str3' => 'I want to return this...',		),	),);

And you want to output 'str1.str2.str3'? That could be a recursive function, but a loop could also do it. The code you posted above isn't actually recursive, those functions don't call themselves.

Link to comment
Share on other sites

That would need to be recursive. It sounds like the best way might be to split the string up using explode again, and then pass that array of tokens to a recursive function which would take the first token and put it in an array as the key, and pass the rest of the tokens and the new array to the recursive function which would keep adding the next level until there were no more tokens.

Link to comment
Share on other sites

Sorry I totally didn't explain that clearly enough. Basically I have a class property called "properties". I use the get() method to return a property from that, using the "." delimiter to signal another nested level in the array. I tried the approach you suggested, but ran into problems merging the new array into the main properties array..Thanks once again..

Link to comment
Share on other sites

Right, the recursive function will need to return the nested arrays to itself, which builds the next level. The function itself only builds one level at a time, and it returns the set built so far to the function which called it, which tacks on the next level, returns that, etc. This could really go either direction too, you could have the function where it builds the array from the bottom up or from the top down.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...