【问题标题】:Multi-dimentional array question: Where does ['children'] come from?多维数组问题:['children'] 从何而来?
【发布时间】:2009-10-04 16:04:43
【问题描述】:

我对多维数组感到困惑。

下面的db、php和html中,我不懂

foreach ($navlist as $key => $list){
foreach ($list as $topkey => $toplist){..

and

foreach ($toplist['children'] as $subkey => $subname){...

而且这段代码也让我感到困惑。是 ['children'] php 吗?

$data[0][$row->parentid]['children'][$row->id] = $row->name;

如果您能解释一下这个 navgation.php,我将不胜感激

提前致谢。

我在 db 中有以下内容。

INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`)  
VALUES (1, 'shoes', 'Shoes for boys and girls.', '', 'active', 7);
INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`) 
VALUES (2, 'shirts', 'Shirts and blouses!', '', 'active', 7);
[...]
INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`)     
VALUES (9, 'test', 'testing', 'Testing!!!!', 'inactive', 0);

模型有以下php

function getCategoriesNav(){
     $data = array();
     $this->db->select('id,name,parentid');
     $this->db->where('status', 'active');
     $this->db->orderby('parentid','asc');
     $this->db->orderby('name','asc');
     $this->db->groupby('parentid,id');
     $Q = $this->db->get('categories');
     if ($Q->num_rows() > 0){
       foreach ($Q->result() as $row){
            if ($row->parentid > 0){
                $data[0][$row->parentid]['children'][$row->id] = $row->name;

            }else{
                $data[0][$row->id]['name'] = $row->name;
            }
        }
    }
    $Q->free_result(); 
    return $data; 
 }

和控制器/navigation.php

  if (count($navlist)){
  echo "<ul>";
  foreach ($navlist as $key => $list){
    foreach ($list as $topkey => $toplist){
        echo "<li class='cat'>";
        echo anchor("welcome/cat/$topkey",$toplist['name']);
        echo "</li>\n";
        if (count($toplist['children'])){
            foreach ($toplist['children'] as $subkey => $subname){
                echo "\n<li class='subcat'>";
                echo anchor("welcome/cat/$subkey",$subname);    
                echo "</li>";
            }
        }
    }
  }
  echo "</ul>\n";
}

这将产生以下html

<ul>
    <li class='cat'>
        <a href="http://127.0.0.1/codeigniter_shopping/welcome/cat/7" title="clothes">
            clothes
        </a>
    </li>
    <li>
        ...
    </li>
    <li class='subcat'>
        <a href="http://127.0.0.1/codeigniter_shopping/welcome/cat/5" title="toys">
            toys
        </a>
    </li>
 </ul>

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    基本结构是这样的:

    foreach ($navlist as $key => $list)
    

    这意味着遍历数组$navlist,并且对于$navlist中的每次,使用键$key和值$list,执行foreach循环的主体。

    一个更简单的例子可能会有所帮助。

    $numbers = array();
    $numbers[0] = "Zero";
    $numbers[1] = "One";
    $numbers[2] = "Two";
    
    foreach($numbers as $number => $text) {
        echo $number." is written ".$text."\n";
    }
    

    输出(除非我在某个地方搞砸了)是:

    0 is written Zero
    1 is written One
    2 is written Two
    

    然后你给出的代码只有多个嵌套循环,所以最外面的for循环中的$list本身就是一个关联数组,然后循环遍历,并且存储在该关联数组中的值也是数组。

    您可能会发现阅读 foreach 文档很有启发性。

    【讨论】:

      猜你喜欢
      • 2014-01-12
      • 2016-07-22
      • 2011-04-24
      • 2018-01-05
      • 2010-11-18
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      相关资源
      最近更新 更多