Please take a look at this depth-first search code, why can't you find it with a reference?

function search(&$node, &$forest, $id)
{
    $node = null;
    $len = count($forest);
    for ($i=0; $i<$len; PP$i)
    {
        if ($forest[$i]["id"] == $id)
        {
            $node = &$forest[$i]; //"&" 
            return;
        }

        search($node, $forest[$i]["children"], $id);
        if ($node) { return; }
    }
}

$forest = array(
    array(
        "id" => 1,
        "pid" => 0,
        "children" => array(
            array(
                "id" => 11,
                "pid" => 1,
                "children" => array(),
            ),
        ),
    ),
    array(
        "id" => 2,
        "pid" => 0,
        "children" => array(),
    ),
);

search($node, $forest, 11);
echo json_encode($node);
Php
Apr.25,2022

Before parsing this phenomenon, take a look at the following code to see whether the parameter $params_1 outputs params_1 or params_2

.
function test(&$p1, &$p2)
{
    $p1 = 'params_1';
    $p1 = &$p2;
}
$params_2 = 'params_2';
test($params_1, $params_2);
echo $params_1;

result

params_1

parse

suppose $params_1 , $params_2 content resides in M1 , m2

  1. executing the function test generates two local variables $p1 , $p2 , but these two local variables are equivalent to pointers, so $params_1 and $p1 point to M1 , $params_2 and $p2 point to m2 .
  2. $p1 = 'params_1'; is equivalent to $params_1 reassignment, and now the value of M1 is params_1
  3. $p1 = & $p2; is equivalent to $p1 points to memory m2 , then $params_1 points to M1 , $params_2, $p1, $p2 points to m2, M1 is still params_1 , so printing $params_1 is params_1 .

main question

the main question of the title can be resolved by using the above analytic solution, $node = & $forest [$I]; this code changes the direction of the local variable $node (no assignment operation), but does not change the search ($node, $forest, 11); the value of the $node variable in this code

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3ac04-2c230.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3ac04-2c230.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?