Marcus
Well-known member
This is the old school recursive search for a node=20 within the grouped nodes:
PHP SPL is much cleaner, but also more than 100x slower:
Do you have any idea why its so much slower, and if SPL is in general slower than using custom made PHP 4 code?
PHP:
function search($array, $key, $value)
{
$results = array();
search_r($array, $key, $value, $results);
return $results;
}
function search_r($array, $key, $value, &$results)
{
if (!is_array($array))
return;
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
search_r($subarray, $key, $value, $results);
}
$outputArray = search($groupedNodes, 'node_id', 20);
PHP:
$arrIt = new RecursiveIteratorIterator(new RecursiveArrayIterator($groupedNodes));
foreach ($arrIt as $key =>$sub) {
$subArray = $arrIt->getSubIterator();
if (isset($subArray['node_id'])) {
if ($subArray['node_id'] === 20) {
$outputArray[] = iterator_to_array($subArray);
}
}
}