I am trying to recurse through an array while also occasionally adding more data to it. The problem I am having is that when I add new data, the array pointer is reset.
sample mock-code:
$datalist = array("a", "d", "g", "l");
while (current($datalist)){ //while there is still a current array item
$v=current($datalist); //current pointer in the array
$n=key($datalist); //put the key into $n
if ($n=="3") { //this causes an endless loop because pointer is reset
$newlist = array("4"=>"z", "x", "c", "v");
$datalist = ($datalist + $newlist); //combines two arrays
}
echo" $n=>$v\n";
next($datalist); //advance the pointer to the next item in the array
}
array_merge() is no good becasue my keys need to be numeric and the later value will not overwrite the original value (which I want), but will be appended.
foreach() operates on a copy of the array so I can't use it.
Any ideas?