Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter when does result_array() return a single or multi dimensional array?

On returning $query->result_array();
Sometime I get multiple single arrays like this:

Array
(
    [user_id] => 32
    [username] => johnd
    [cat_id] => 7
)
Array
(
    [user_id] => 33
    [username] => Janed
    [cat_id] => 6

)

While sometimes i get multidimensional arrays like this:

Array
(
    [0] => Array
        (
            [user_id] => 33
            [username] => Janed
            [cat_id] => 6
        )

    [1] => Array
        (
            [user_id] => 32
            [username] => Johnd
            [cat_id] => 7
        )

)

Is it something to do with the query, is there a specificreason for this?

like image 500
Saff Avatar asked Oct 22 '22 17:10

Saff


1 Answers

$query->result_array() always returns you a 2D array (unless the database returns no results, then it returns an empty array).

It returns you an array of "result" arrays. Each result array contains that row's fields.

Docs: http://ellislab.com/codeigniter/user-guide/database/results.html

like image 139
Rocket Hazmat Avatar answered Oct 27 '22 07:10

Rocket Hazmat