Getting array key value in codeigniter

My controller has the following line:

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

      

This data prints out the following using the print_r function

    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
            )

    )

)

      

I want to use the value stored in the [catid] key and I am trying to do something like: $ data ['faq'] ['catid'] to get this value in the controller (I want to make another selection with this value) But I am getting with this error message: Undefined index: catid

Anyone can help me get the value of ['catid'] ???

Regards, Zoran

+3


source to share


2 answers


Its 3-dimensional array u takes a close look at the two elements in the array faq

. You should write something like this: $data['faq'][0]['catid']

or$data['faq'][1]['catid']



+3


source


The way you are accessing the array is wrong , you are not given the element index at the second level. The correct way to use it as you are doing is to do

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

      



However, this will only show one faqid

at a time, and it is not that useful when you are iterating. So a good way would be this way.

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

      

+1


source







All Articles