0

I just don't understand, I have code like this:

$this->ci->db->select('liked_posts, liked_comments');       
$q = $this->ci->db->get_where('users_fav', array('usr_id' => $this->_usrId));
$result = $q->result_array();

And when I, as always, tried to put it into foreach loop.. it's just didn't work.. Because in $result I've got and array where 2 more arrays where stored (table fields) so to work in foreach loop it would look like this:

foreach ($result[0] as $value)

not:

foreach ($result as $value)

And I was looking for my mistake very long.. Maybe I really did something wrong... Or is it a bug?

edit: print_r($result);

Array
(
    [0] => Array
        (
            [liked_posts] => a:0:{}
            [liked_comments] => a:0:{}
        )

)

edit2:

But shouldn't it be like this:

Array
            (
                [liked_posts] => a:0:{}
                [liked_comments] => a:0:{}
            )
2
  • define 'does not work', is there an error? The printed $result looks like what is to be expected... Commented Sep 19, 2013 at 14:14
  • what output you want ? Commented Sep 19, 2013 at 14:16

2 Answers 2

2

so you can do this

foreach($result as $value)
{
    echo $value['fav_posts'];
}

no problem with that.

Sign up to request clarification or add additional context in comments.

Comments

0

When using $result = $q->result_array(); you will get a multidimentional array as you have now.:

foreach( $resuls as $key => $each ){
    echo "result : ".$each['column_name'];
}

but if you have just a single row fetched you would likely use $result = $q->row_array(); which will return a single dimentional array. And you can directly use like this:

echo $results['column_name'];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.