2015-09-10 43 views
0

result_array返回值但在codeigniter中不返回$ key,如果我在視圖中手動添加數據數組,它返回$ key,爲什麼它不會回顯$關鍵。

型號::

<?php 

class Admin_user extends CI_Model 
{ 

    public function list_rows() 
    { 
     $query = array(); 
     $query = $this->db->get('content'); 
     return $query->result_array(); 

    } 

} 

查看::

<?php 
foreach ($get_all_content as $key => $values) 
{ 
    $title = $values['title']; 
    echo "<th>" . $key . "</th>"; 
} 
?> 

控制器::

$data['get_all_content'] = $this->admin_user->list_rows(); 

的print_r ::

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [menu_id] => 12 
      [title] => Register Domain name for free 
      [sub_title] => This is the sub_title 
      [content] => this is the content description 
      [description] => 
      [section] => 
     ) 
) 
+0

你'的print_r($數據[ 'get_all_content'];'?你在哪裏得到你的'print_r()'? – aldrin27

+0

是的,它返回,我在視圖Array([0] =>陣列([id] => 1 [menu_id] => 12 [title] =>免費註冊域名[sub_title] =>這是sub_title [內容] =>這是內容描述[description] => [section] =>)) – wicky

+0

但是,當你這樣做'$ title = $ values ['title'];'你會得到標題? – aldrin27

回答

1

試試這個:

$yourdata = [0 => 
     ['id' => 1, 'menu_id' => 12, 'title' => 'Register Domain name for free','sub_title' => 'This is the subtitle', 'content' => 'this content', 'description' => 'description', 'section' => 'sect'] 
]; 

    foreach($yourdata as $key => $val) 
    { 
     foreach($val as $key2 => $newVal) 
     { 
      echo '<pre>'; 
      var_dump($key2, $newVal); 
       //or echo it echo $key2 and echo $newVal 
     } 
    } 

導致輸出:

string(2) "id" 
    int(1) 

    string(7) "menu_id" 
    int(12) 

    string(5) "title" 
    string(29) "Register Domain name for free" 

    string(9) "sub_title" 
    string(20) "This is the subtitle" 

    string(7) "content" 
    string(12) "this content" 

    string(11) "description" 
    string(11) "description" 

    string(7) "section" 
    string(4) "sect" 
+0

它的工作原理:-)感謝您的代碼示例,爲什麼第一個循環中的$ key不起作用? – wicky

+0

沒問題。樂意效勞。也許你可以將其標記爲已接受。 TNX。 – aldrin27

相關問題