3

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?

6
  • 1
    Isn't that first example also an array of arrays? How do you reference the second one? Also maybe you are calling result_array() and row_array(). See ellislab.com/codeigniter/user-guide/database/results.html Commented Feb 15, 2013 at 14:31
  • The first one just returns the each column in an array(which makes life easy btw), the second returns the columns in an array withing an array. Yh i checked that as well but both of them are result_array() :( Commented Feb 15, 2013 at 14:40
  • 2
    What do you mean by "sometimes"? How are you iterating / printing the result? Are you sure you are calling result_array? Commented Feb 15, 2013 at 14:57
  • @RocketHazmat: that's a very good question. I ran some tests and i found out that it does indeed depend on how the result is printed. Commented Feb 15, 2013 at 15:02
  • @Saff: result_array always returns a 2D array. Depending on how you loop/view it, it may look different, but it's not. Commented Feb 15, 2013 at 15:05

1 Answer 1

3

$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

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

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.