phparrayscodeigniterresultset

When does CodeIgniter's result_array() method 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 other times, 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 specific reason for this?


Solution

  • $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