在codeigniter中获取数组键的值

时间:2012-03-19 10:09:01

标签: codeigniter

我的控制器中有以下行:

$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']的价值???

关心,佐兰

2 个答案:

答案 0 :(得分:3)

它的三维数组你仔细观察faq数组中有两个元素。您必须写下这样的内容:$data['faq'][0]['catid']$data['faq'][1]['catid']

答案 1 :(得分:1)

您访问阵列的方式是不正确,您在第二级缺少项目索引。正如你所做的那样使用它的正确方法是

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

但是,这一次只显示一个faqid,并且在迭代时不太有用。所以,一个好方法就是这样。

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