Help! Recursive array search - return by reference.
Posted: Wed Jan 28, 2009 3:04 am
Hi all!
We all know how easy it is to return variables by reference. Consider the following code:
The above sample works as expected, the middle element from $array is returned, and modified via $something. The changes reflect in $array because getSomething returned the value by reference.
I'm trying to take the above one step further...a recursive array searh function as follows:
In my code that calls the above function, it returns the array element successfully, but NOT by reference as I expected. Changes do not reflect in the original.
I am suspecting line 37 in the above snippet has a problem.
I call it like this:
and then try to modify it like this:
Can anybody possibly see what I am doing wrong here?
Thanks in advance!!!
We all know how easy it is to return variables by reference. Consider the following code:
Code: Select all
<?php
$array = array('alpha','beta','charley');
function &getSomething(&$array) {
$result =& $array[1];
return $result;
}
$something =& getSomething($array);
$something = 'funky chicken';
?>
<pre>
<?php print_r($array);?>
</pre>I'm trying to take the above one step further...a recursive array searh function as follows:
Code: Select all
<?php
/**
* Contains basic functionality for dealing with arrays.
*/
class ArrayUtil
{
/**
* Performs a recursive search on the given associative array $ar. It will return, by reference, they element found as
* indicated by the given $searchKey and $findValue.
*
* @param array $ar The associative array to search through.
* @param string $searchKey The key value used for matching.
* @param string $findValue The value for the key to match.
*
* @return Returns BY REFERENCE, the array element if found! Thus, you can modify it directly.
*/
public static function &findRecursive(&$ar, $searchKey, $findValue) {
$result = null; // Contains the located element, null if none.
// Check parameters
if(!isset($ar, $searchKey, $findValue) || !is_string($searchKey) || !is_string($findValue)) {
throw new Exception('ArrayUtil::findRecursive() call has missing or invalid parameters!');
}
if (!is_array($ar)) {
throw new Exception('ArrayUtil::findRecursive() cannot perform a recursive search. $ar is not an array!');
}
// Start traversing the given array
foreach ($ar as $key => $item) {
// Evaluate, see if this is our "Wanted Bandit"
if ($key == $searchKey && $item[$searchKey] == $findValue) {
$result =& $ar;
}
// Is this an array? Then recurse...go deeper...penetrate...and do not be afraid!!!
if (is_array($item)) {
$result =& self::findRecursive($item, $searchKey, $findValue);
}
// Has our element been found?
if (isset($result)) {
return $result;
}
}
// Nothing found...sad but true!
return null;
}
}
?>I am suspecting line 37 in the above snippet has a problem.
I call it like this:
Code: Select all
$parent =& ArrayUtil::findRecursive($result, 'id', $value['parent_id']);Code: Select all
if (!isset($parent['children'])) {
$parent['children'] = array();
}
$parent['children'][] = $value;Thanks in advance!!!