Çok boyutlu bir dizi soru: ['çocuk'] nereden geliyor?

1 Cevap php

Ben çok boyutlu dizi hakkında karıştı.

Aşağıdaki db, php ve html, ben kullanımını anlamak yok

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

and

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

Ve ayrıca bu kod benim için kafa karıştırıcı. ['Çocuk'] php mi?

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

Bu navgation.php açıklayabilir ben takdir edecektir

Şimdiden teşekkürler.

Ben db destekçilere sahiptir.

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);

Model aşağıdaki php sahiptir

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; 
 }

Ve kontrolörü / 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";
}

Bu, aşağıdaki html üretecek

<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>

1 Cevap

Temel yapısı, şudur:

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

Bu $navlist dizi yineleme anlamına gelir ve her zaman için $navlist, tuşu ile $key ve değeri $list, vücut ve yürütmek foreach döngü.

Daha basit bir örnek yardımcı olabilir.

$numbers = array();
$numbers[0] = "Zero";
$numbers[1] = "One";
$numbers[2] = "Two";

foreach($numbers as $number => $text) {
    echo $number." is written ".$text."\n";
}

Bu çıkış (I yerde mahvettin sürece), olduğunu:

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

Eğer verdiğiniz kod sadece birden çok iç içe döngüler vardır, bu yüzden $list için döngü dıştaki sonra üzerinden çevrilir birleşmeli dizi, kendisi olduğunu ve ilişkisel dizi içinde saklanan değerler de dizilerdir .

Sen foreach belgelerine öğretici okuma bulabilirsiniz.