Open lovecn opened 7 years ago
https://segmentfault.com/q/1010000006866791 $arr= array( '1'=>array('id'=>1, 'pid'=>0,), '2'=>array('id'=>2, 'pid'=>0,), '3'=>array('id'=>3, 'pid'=>1,), '4'=>array('id'=>4, 'pid'=>2,), '5'=>array('id'=>5, 'pid'=>4), '6'=>array('id'=>6, 'pid'=>5,), '7'=>array('id'=>7, 'pid'=>3), '8'=>array('id'=>8, 'pid'=>4,), '9'=>array('id'=>9, 'pid'=>7), );
//以ID为键值格式化 foreach ($arr as $key => $value) { $list[$value['id']]=$value; }
//遍历数组 foreach ($list as $key => $value) { if($value['pid']==0){ $tree[$value['id']]=$value;
}else{
//祖先ID
$ancestor_id='';
//判断祖先数组里面有没有对应的值,有则取出
if(!empty($ancestor[$value['pid']])){
$ancestor_id=$ancestor[$value['pid']];
}else{
$topid=$value['id'];
$child=array();
if(empty($ancestor[$topid])){
$child[]=$topid;
}
//通过循环,一层一层的往上找,找出他们各自的父亲,直到祖宗为止
while($list[$topid]['pid'] > 0)
{
$topid = $list[$topid]['pid'];
if(empty($topid)){
$topid='';
break;
}else{
//祖宗数组里面没有,则放入子孙数组
if(empty($ancestor[$topid])){
$child[]=$topid;
}else{
//祖先已经存在,就不用循环下去了
$topid=$ancestor[$topid];
break;
}
}
}
$ancestor_id= $topid;
//如果找到祖宗,就将子孙数组对应加入到祖宗数组里面
if(!empty($ancestor_id)){
foreach ($child as $k => $v) {
$ancestor[$v]=$ancestor_id;
}
}
}
if(!empty($ancestor_id)){
$tree[$ancestor_id]['child'][$value['id']]=$value;
}
}
} echo '
'; var_dump($tree); Array ( [1] => Array ( [id] => 1 [pid] => 0 [child] => Array ( [3] => Array ( [id] => 3 [pid] => 1 )[7] => Array ( [id] => 7 [pid] => 3 ) [9] => Array ( [id] => 9 [pid] => 7 ) ) ) [2] => Array ( [id] => 2 [pid] => 0 [child] => Array ( [4] => Array ( [id] => 4 [pid] => 2 ) [5] => Array ( [id] => 5 [pid] => 4 ) [6] => Array ( [id] => 6 [pid] => 5 ) [8] => Array ( [id] => 8 [pid] => 4 ) ) )
)
Array ( [0] => Array ( [id] => 1 [name] => 代码 [parent] => 0 )
[1] => Array
(
[id] => 3
[name] => 动漫
[parent] => 0
)
[2] => Array
(
[id] => 4
[name] => 治愈
[parent] => 3
)
[3] => Array
(
[id] => 5
[name] => 励志
[parent] => 3
)
[4] => Array
(
[id] => 6
[name] => 机战
[parent] => 3
)
[5] => Array
(
[id] => 7
[name] => 百合
[parent] => 3
)
[6] => Array
(
[id] => 8
[name] => 资源
[parent] => 0
)
[7] => Array
(
[id] => 9
[name] => app
[parent] => 8
)
[8] => Array
(
[id] => 10
[name] => 软件
[parent] => 8
)
[9] => Array
(
[id] => 11
[name] => 黑科技
[parent] => 8
)
) https://segmentfault.com/q/1010000007011243 function _data_to_tree(&$items, $topid = 0, $with_id = TRUE) { $result = []; foreach($items as $v) if ($topid == $v['parent']) { $r = $v + ['children' => _data_to_tree($items, $v['id'], $with_id)]; if ($with_id) $result[$v['id']] = $r; else $result[] = $r; }
return $result;
}
function _data_to_tree($items, $topid = 0, $with_id = TRUE) { if ($with_id) foreach ($items as $item) $items[ $item['parent'] ]['children'][ $item['id'] ] = &$items[ $item['id'] ]; else foreach ($items as $item) $items[ $item['parent'] ]['children'][] = &$items[ $item['id'] ];
return isset($items[ $topid ]['children']) ? $items[ $topid ][ 'children' ] : [];
}
$data = [ ['id' => 4, 'parent' => 1 , 'text' => 'Parent1'], ['id' => 1, 'parent' => 0 , 'text' => 'Root'], ['id' => 2, 'parent' => 1 , 'text' => 'Parent2'], ['id' => 3, 'parent' => 2 , 'text' => 'Sub1'], ]; print_r ( _data_to_tree($data, 0) );
在php5,一个对象变量已经不再保存整个对象的值。只是保存一个标识符来访问真正的对象内容。 当对象作为参数传递,作为结果返回,或者赋值给另外一个变量,另外一个变量跟原来的不是引用的关系,只是他们都保存着同一个标识符的拷贝,这个标识符指向同一个对象的真正内容。 class A{ public $num=100; } $a = new A(); $b = $a; $a->num=200; echo $b->num;//200 对象的复制是通过引用来实现的,$a=new A();$b=$a;相当于$a=new A();$b=&$a;
php 奇葩 0=='0' //true 0 == 'abcdefg' //true 0 === 'abcdefg' //false 1 == '1abcdef' //true "0e132456789"=="0e7124511451155" //true "0e123456abc"=="0e1dddada" //false "0e1abc"=="0" //true
"0x1e240"=="123456" //true "0x1e240"==123456 //true "0x1e240"=="1e240" //false 如果switch是数字类型的case的判断时,switch会将其中的参数转换为int类型
$i ="2abc"; switch ($i) { case 0: case 1: case 2: echo "i is less than 3 but not negative"; break; case 3: echo "i is 3"; } $array=[0,1,2,'3']; var_dump(in_array('abc', $array)); //true var_dump(in_array('1bc', $array)); //true var_dump(intval('2')) //2 var_dump(intval('3abcd')) //3 var_dump(intval('abcd')) //0 if(intval($a)>1000) { mysql_query("select * from news where id=".$a) } $array1[] = array( "foo" => "bar", "bar" => "foo", ); $array2 = array("foo", "bar", "hello", "world"); var_dump(md5($array1)==var_dump($array2)); //true 传递一个array时,md5()不会报错,知识会无法正确地求出array的md5值,这样就会导致任意2个array的md5值都会相等。