php getting array levels in the recursion -
i have next tree-array:
$arr = array( 'id' => 1431, 'children' => array( 1432 => array( 'id' => 1432, 'children' => array( 1433 => array( 'id' => 1433, 'children' => array(), ), 1434 => array( 'id' => 1434, 'children' => array(), ), ) ), 1435 => array(), 'id' => 1435, 'children' => array( 1436 => array( 'id' => 1436, 'children' => array( 1437 => array( 'id' => 1437, 'children' => array(), ), 1438 => array( 'id' => 1438, 'children' => array(), ), 1439 => array( 'id' => 1439, 'children' => array(), ), ), ), ), ), );
my task generations array array. output should next:
array( [1] = array( [1432] = ... [1435] = ... ), [2] = array( [1433] = ... [1434] = ... [1436] = ... ), [3] = array( [1437] = ... [1438] = ... [1439] = ... ), )
but output next (without 1346 element):
array( [1] = array( [1432] = ... [1435] = ... ), [2] = array( [1433] = ... [1434] = ... ), [3] = array( [1437] = ... [1438] = ... [1439] = ... ), )
what wrong in function?
public function getgenerations($usertree, $currgeneration = 0, $result = array()) { print_r($usertree); $currgeneration++; if (!empty($usertree) && !empty($usertree['children'])) { foreach($usertree['children'] $k => $v) { $curruser = $v; unset($curruser['children']); $result[$currgeneration][$k] = $curruser; $result += $this->getgenerations($v, $currgeneration, $result); } } return $result; }
i call function this: $res = getgenerations($arr);
thank in advance. sorry english.
you pass result array reference instead of returning , joining local result array:
public function getgenerations($usertree, $currgeneration = 0, &$result = array()) { print_r($usertree); $currgeneration++; if (!empty($usertree) && !empty($usertree['children'])) { foreach($usertree['children'] $k => $v) { $curruser = $v; unset($curruser['children']); $result[$currgeneration][$k] = $curruser; $this->getgenerations($v, $currgeneration, $result); } } return $result; }
Comments
Post a Comment