PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
function go(&$a,$count)
{
/**
* return reference to a "node" who place in $count steps
* from the beging.
*
* &$a - reference to array to prevent copy of it.
* $count - number of step to move.s
*/
$arr=&$a; //define local var to move in the array.
//and start in first "node".
for($i=0;$i<$count;$i++)
{
$arr=&$arr[1]; //set the local var to point the next array.
}
return $arr; //return the desire reference
}
$arr=go($a,2); //go two step
$arr[0]=6; //set the value of it to 3.
var_dump($a);
function &getNodeAtDepth(&$a, $count)
{
/**
* Return reference to the node that is $count levels
* deep.
*
* $a - array to process, in format of array(0=>integer, 1=>array)
* $count - number of levels to step down
*/
$arr =& $a; // define local reference to array
for($i=0; $i < $count; $i++)
{
$arr =& $arr[1]; //set the local var to point the next array.
}
return $arr; //return the desired reference
}
$a=array();
$a[0]=1;
$a[1]=array();
$a[1][0]=5;
$a[1][1]=array();
$a[1][1][0]=8;
$arr =& getNodeAtDepth($a, 2); // go two depths down: NEEDS & operator
$arr[0] = 6; //set the value
var_dump($a);
/*
expect:
array(2) {
[0]=>
int(1)
[1]=>
&array(2) {
[0]=>
int(5)
[1]=>
&array(1) {
[0]=>
int(6)
}
}
}
*/