Home > Backend Development > PHP Tutorial > 看不懂啊请给小弟我详细解释下

看不懂啊请给小弟我详细解释下

WBOY
Release: 2016-06-13 12:01:05
Original
1073 people have browsed it
MyException - 代码迷
首页
  • Web开发
  • Android
  • J2EE
  • PHP
  • Linux/Unix
  • MySql
  • C#
  • 更多...
当前位置: 代码迷 » PHP » 看不懂啊请给小弟我详细解释下

看不懂啊请给小弟我详细解释下

www.MyException.Cn,网友分享于:2014-06-30   浏览:3次
看不懂啊,请给我详细解释下.
foreach ($categories as $key => $value) {<br />    $upid = $value['upid'];<br />    $categories[$key]['level'] = 0;<br />    if ($upid && isset($categories[$upid])) {<br />	$categories[$upid]['children'][] = $key; // 这个三维数组是什么意思? 这个$key值是怎么赋予这个三维数组的/<br />	while ($upid && isset($categories[$upid])) {<br />	    $categories[$upid]['allnodes'][] = $key;<br />	    $categories[$upid]['count'] += $categories[$key]['count']; //这个又是怎计算的, ??<br />	    $categories[$key]['level'] += 1;<br />	    $categories[$key]['parent'][$upid] = $categories[$upid]['catname']; //这个,这个也不懂<br />	    $upid = $categories[$upid]['upid']; //这个也不懂.<br />	}<br />	$categories[$key]['parent'] = array_reverse($categories[$key]['parent'], true);<br />    }<br />    if ($categories[$key]['level'] == 0) {<br />	$rootcategories[] = $key;<br />    }<br />}<br />
Copy after login

------解决思路----------------------
$categories[$upid]['children'][] = $key;

例如 $upid=1, $key=2;
$categories[$upid]['children'][] = $key;
等于
$categories[1]['children'] 是一个数组,然后$key作为插入到数组最后。
执行完后
$categories = array(
    1 => array(
        'children' => array(2)
    )
);


categories[$upid]['count'] += $categories[$key]['count'];
等于
categories[$upid]['count'] = categories[$upid]['count'] + $categories[$key]['count'];


$categories[$key]['parent'][$upid] = $categories[$upid]['catname'];  
$upid = $categories[$upid]['upid'];
这两句都是直接赋值。




------解决思路----------------------
$categories[$upid]['children'][]  = xxx 表示在$categories[$upid]['children'] 数组插入一个新元素。
例如
$arr = array('key'=>array());

$arr['key'][] = 1;
$arr['key'][] = 2;
$arr['key'][] = 3;

执行后.
$arr = array(
    'key' => array(1,2,3)
);

$categories[$upid]['count'] = $categories[$upid]['count'] + $categories[$key]['count'];
categories[$upid]['count'] 與 $categories[$key]['count'] 是應該是兩個數值,所以可以相加。
你可以print_r($categories); 看看$categories裡面是什麼。

$categories[$key]['parent'][$upid] = $categories[$upid]['catname']; 

這個可以簡單點
$key = 'a';
$upid = 'b';

$categories[$key]['parent'][$upid] = 1;
$categories[$upid]['catname'] = 2;
相當於
$categories['a']['parent']['b'] = 1;
$categories['b']['catname'] = 2;

執行
$categories[$key]['parent'][$upid] = $categories[$upid]['catname'];  
相當於
$categories['a']['parent']['b'] = $categories['b']['catname'];  

所以
$categories[$key]['parent'][$upid] = 2;




------解决思路----------------------
学编程,就不能怕动手
在这段代码的前后分别 print_r($categories);
一切净收眼底
------解决思路----------------------
引用:
学编程,就不能怕动手
在这段代码的前后分别 print_r($categories);
一切净收眼底


错别字哇。。。应该是尽收眼底
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template