|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-06-29 17:18 UTC] colder@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 01:00:01 2025 UTC |
Description: ------------ Made a simple recrusion function, sorting id by parentid, unsetting sorted values to shorten time of next recrusions. Unsetting inside foreach with deep arrays does work(!), but with simple array unset somehow stops the recrusion (first cicle will never finish its loop, the last loop will be made by latest child). Tested with PHP 5.2.6 on Debian-40r3-amd64 and Windows Vista 32 Reproduce code: --------------- <?php function recrusion($array, $return, $pid=0, $level=0){ foreach($array as $id=>$parent){ if( $parent===$pid ){ $return[$id]= $level; unset($array[$id]); /* remove this to get correct results */ recrusion(&$array, &$return, $id, $level+1); } } } $return= array(); $array= array(1=>0,2=>1,3=>2,4=>3,5=>2,6=>2,7=>6,8=>6,9=>0,10=>0); recrusion($array, &$return); var_dump( $return ); ?> Expected result: ---------------- array(10) { [1]=> int(0) [2]=> int(1) [3]=> int(2) [4]=> int(3) [5]=> int(2) [6]=> int(2) [7]=> int(3) [8]=> int(3) [9]=> int(0) [10]=> int(0) } Actual result: -------------- array(6) { [1]=> int(0) [2]=> int(1) [3]=> int(2) [4]=> int(3) [9]=> int(0) [10]=> int(0) }