php - Can anyone clearly explain this infinite hierarchical function?
学习ing
学习ing 2017-06-29 10:08:08
0
6
708

I can use it, but I can’t understand it every time I look at it. Can anyone explain and clear it up? &How to understand reference assignment here?

$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' => 3, 'name' => '长丰县'),
    5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);
function getTree($items){
    $tree = array();
    foreach($items as $item){
        if(isset($items[$item['pid']])){
            $items[$item['pid']]['son'][] = &$items[$item['id']];
        }else{
            $tree[] = &$items[$item['id']];
        }
    }
    return $tree;
}
学习ing
学习ing

reply all(6)
習慣沉默

Mainly the use of references, which can simplify the test:

  • When quoted:

$item1= array('id' => 1, 'pid' => 0, 'name' => '安徽省');
$item2= array('id' => 2, 'pid' => 0, 'name' => '浙江省');
$item3=array('id' => 3, 'pid' => 1, 'name' => '合肥市');
$tree[] = &$item1;//注意引用
$item1['son']=&$item2;//注意引用
$item2['son']=$item3;
print_r($tree);
  • Without citation:

$item1= array('id' => 1, 'pid' => 0, 'name' => '安徽省');
$item2= array('id' => 2, 'pid' => 0, 'name' => '浙江省');
$item3=array('id' => 3, 'pid' => 1, 'name' => '合肥市');
$tree[] = $item1;//注意无引用
$item1['son']=$item2;//注意无引用
$item2['son']=$item3;
print_r($tree);

When there is a reference, changes to the sub-elements will be displayed in the entire array, but when there is no reference, changes to the sub-elements will have no impact on the entire array.

巴扎黑

Is there no one here? Please explain

迷茫

is like this, items in the brackets of foreach => a and items => b in the loop body exist in two places in memory. After using &, b will point to a i.e. the real items.

But after php7, foreach has changed a bit => php7 foreach is not backward compatible

世界只因有你

Reference assignment means that the left side of the assignment points directly to the area in the memory where the value is stored, instead of opening up a new space to receive a copy of the data.

So, in the parent-child level relationship, reference assignment directly points the ['son'] in the parent element directly to the storage area of ​​the child element, instead of just storing the value. Each parent element points ['son'] to the storage area of ​​the corresponding child element. In this way, it has actually been connected to form a tree structure in the memory. Since all parent elements have ['son'] all point to the memory area of the child element , so the parent-child relationship in the output array is basically the same as the data relationship tree in the memory.

世界只因有你

Reference @vishun

$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' => 3, 'name' => '长丰县'),
    5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);

//&引用赋值在这儿如何理解?
$items[1]['son'][] = &$items[3];
$items[3]['son'][] = &$items[2];
print_r($items[1]);
//简化了结果,可以直接看son
//Array
//(
//    [id] => 1,[pid] => 0,[name] => 安徽省,
//    [son] => Array(
//        [0] => Array(
//            [id] => 3,[pid] => 1,[name] => 合肥市,
//            [son] => Array(
//                [0] => Array(
//                    [id] => 2,[pid] => 0,[name] => 浙江省
//                )
//            )
//        )
//    )
//)

//无限分级函数么?
function getTree($items){
    $tree = array();
    foreach($items as $item){
        if(isset($items[$item['pid']])){//关键是看这个判断,是顶级分组就给$tree,不是的话继续拼凑子分组(结合上述&用法)
            $items[$item['pid']]['son'][] = &$items[$item['id']];

        }else{
            $tree[] = &$items[$item['id']];
        }
    }
    return $tree;
}
大家讲道理

After seeing the above answers, I still want to answer. The use of references in PHP is similar to the usage of pointers in C/C++. It is equivalent to operating the pointer of this variable. In this case, operating the reference variable in the function will also trigger The variable itself changes.
References can bring some benefits: because they operate "pointers" directly, they are very efficient and will not cause unnecessary waste of memory and consume the performance of opening up memory;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!