Jump to content

Return the value of an array member


boen_robot

Recommended Posts

Is there a function that can return the value of an array member in a multi dimensional array? That is, look thru the whole array in all of it's dimensions and return the value of a specified key?That is, for example if we have

Array(	[0] => Array		(			[0] => 			[1] => Array				(					[myKey] => myValue				)			[2] => anotherOne			[3] => Array				(					[yes] => no					[myKey] => ohNo				)		))

how can I get a single call with "myKey" as an argument to return

Array(	[0] => myValue	[1] => ohNo)

?I tryed looking at the PHP manual, but couldn't find anything like that. The closest was array_search, which does that, but for array values to return keys. I want the opposite though.The dimensions of the array I need this for are known, however looping on it only to find a few values seems... well... isn't it very performance costly? And what if the dimensions weren't known?

Link to comment
Share on other sites

Try this:

function findValueByKey( $searchKey, $array, &$returnArray ){	// Check the arguments... (Basic)	if (empty( $searchKey ) || empty( $array ) || !is_array( $array ) || !is_array( $returnArray ))		return false	// Go thru the values...	foreach ($array as $key => $value) {		// Check if the key matches		if ($key == $searchKey) {			// We could do something like checking the type etc			$returnArray[] = $value;		}		// Check if there's children..		if (is_array( $value ))			// Make a recursive call whith the chidren(s)			findValueByKey( $searchKey, $value, $returnArray ) or return false;	}	return true;}

It takes the key to look for, the array to search in and an array to store the found values in...Usage:

findValueByKey( 'myKey', $theArray, $result ) or die('Error');

Note: I don't know if this is the best way (when it comes to speed and perfomance)You could also rewrite it to return the array, but I think that the function would be much slower..You could use a function like this though..

function getValueByKey( $key, $array ){	$returnArr = array();	if (findValueByKey( $key, $array, $returnArr ))		return $returnArr;	else		return false;}

Link to comment
Share on other sites

So I guess there's not a built in function for that, right? Oh well... I guess I'll stick with what I previously made (looping thru the first dimension of the array, and returned the proper value for the key from then on). As I said, in my current case, at least the dimensions are known.But I am going to stash away this function of yours for when I don't know the dimensions :) .Btw, what's the "&" for in "&$returnArray "? I remember reading about it once, but can't remember what and where.

Link to comment
Share on other sites

The ampersand (&) makes the variable a refference (almost similar to pointers/references in C/C++).It tells PHP to not create a new variable called $returnArray on each call to the function (which would mean that the changes to it wont be kept when the function ends). The name $returnArray will instead be a reference to the variable given to functioncall..That became more complex than i intended (you can read more in the manual)Here's an small example:

function addOne( &$val ){   $val++;}$a = 2;addOne( $a );echo $a; //Outputs 3

Hope that helped!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...