2012-03-19 64 views
3

值我在我的控制器下面一行:越來越陣列關鍵的笨

$data['faq'] = $this->faqModel->get(); 

此數據印刷使用的print_r

Array 
(
[faq] => Array 
    (
     [0] => Array 
      (
       [faqid] => 12 
       [catid] => 122 
       [question] => How this CMS works 
       [question_en] => How this CMS works 
       [answer] => How this CMS works? 
       [answer_en] => How this CMS works? 

       [sorder] => 2 
       [visible] => 1 
      ) 

     [1] => Array 
      (
       [faqid] => 8 
       [catid] => 121 
       [question] => How does the design cost? 
       [question_en] => How does the design cost? 
       [answer] => How does the design cost? 

       [answer_en] => How does the design cost? 

       [sorder] => 1 
       [visible] => 1 
      ) 

    ) 

) 

我想使用存儲在值以下[catid]鍵,我試圖做這樣的事情: $ data ['faq'] ['catid']在控制器中獲取該值(我想再次使用該值進行選擇)但是我得到與此錯誤消息:未定義的索引:catid

任何人都可以幫助我獲得['catid']的價值?

問候,卓然

+1

你缺少前CATID $數據指數[ '常見問題解答'] [0] [ 'CATID'] – arma 2012-03-19 10:11:04

+1

感謝ARMA,它現在的作品。 – Zoran 2012-03-19 10:26:26

回答

3

其3維陣列倒帶密切存在faq陣列的兩個元素。你必須寫的東西是這樣的:$data['faq'][0]['catid']$data['faq'][1]['catid']

1

您正在訪問的陣列的方式是不正確,你缺少的第二個層次的項目指標。用它作爲你正在做正確的方法是做

echo $data['faq'][0]['faqid']; //faqid of the first item 

然而,這僅會一次一個faqid,它不是那麼有用,當你迭代。所以,一個好方法就是這樣。

foreach($data['faq'] as $value) { 
echo $value['faqid']; 
}