How does this piece of php code execute logic? can you tell me if I don't understand it?

     $items = array(
            1 => array("id" => 1, "pid" => 0, "name" => ""),
            2 => array("id" => 2, "pid" => 0, "name" => ""),
            3 => array("id" => 3, "pid" => 1, "name" => ""),
            4 => array("id" => 4, "pid" => 2, "name" => ""),
            6 => array("id" => 6, "pid" => 3, "name" => ""),
            5 => array("id" => 5, "pid" => 2, "name" => ""),
            7 => array("id" => 7, "pid" => 3, "name" => "")
        );
    print_r($this->genTree5($items));
    //
    public function genTree5($items) {
        foreach ($items as $item)
            $items[$item["pid"]]["son"][$item["id"]] = &$items[$item["id"]];
        return isset($items[0]["son"]) ? $items[0]["son"] : array();
    }

the infinite classification conversion n-dimensional array I found on the Internet is completed in 5 lines. I can"t understand it. I can"t see where he returns, but he"s so awesome. Trace and debug, $items [$item ["pid"]]" son"]; just repeat it here. Then you can output a multi-dimensional array, which feels so powerful. Who can help me to analyze, this is how he executes the logic.

I"m not good at it, and I"m a little stupid. I just like code.

Array
(
    [1] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => 
            [son] => Array
                (
                    [3] => Array
                        (
                            [id] => 3
                            [pid] => 1
                            [name] => 
                            [son] => Array
                                (
                                    [6] => Array
                                        (
                                            [id] => 6
                                            [pid] => 3
                                            [name] => 
                                        )

                                    [7] => Array
                                        (
                                            [id] => 7
                                            [pid] => 3
                                            [name] => 
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 2
            [pid] => 0
            [name] => 
            [son] => Array
                (
                    [4] => Array
                        (
                            [id] => 4
                            [pid] => 2
                            [name] => 
                        )

                    [5] => Array
                        (
                            [id] => 5
                            [pid] => 2
                            [name] => 
                        )

                )

        )

)
Oct.26,2021

pass a value by reference

The

$items loop changes itself

for example, the value passed by the first loop reference becomes $arr,. The second loop, that is, the loop's $arr, means that the array of the current loop is not looped once, and the initial data is the result of the previous loop.

becomes like this for the first time, and so on

$arr=[
  [
      "id"=>1,
      "pid"=>0,
      "name"=>'',
      "son"=>[
          [
              "id"=>3,
              "pid"=>1,
              "name"=>""
          ]
      ]
  ],
  [
      "id"=>1,
      "pid"=>0,
      "name"=>'',
  ]
];


does not use recursion, it uses references.

you can print out the $items after each loop and see how it works.

The basic logic of

is that each loop finds its upper level for the current element and passes the current element reference to the son of the previous level.
finally prints out the top-level element with a pid of 0 in $items.


this implementation is so powerful that if there is more data, it will be hundreds of times faster than recursion. But I still can't understand it. I feel dizzy because of the array nesting. I also know what a citation is. I've used it before. Is to pass the address of the variable, for example, to a subfunction, and if the subfunction changes this value, then whoever passes it to him will change. Is that they share a storage address. I know the principle, but I still don't quite understand this.


function genTree5($items) {
    foreach ($items as $item){
        $items[$item['pid']] ['son'][$item['id']] = &$items[$item['id']];
        echo "
";
        var_dump($items);
        echo '-------------------------------------------------------------';
    }
    return isset($items[0]['son']) ? $items[0]['son'] : array();
}
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-1bf0d45-31d24.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-1bf0d45-31d24.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?