0

I want Codeigniter produce array result in active record, just like mysql_fetch_array() does in standard PHP.

Let's just say, we have this table in our mysql database, called students.

+-----+------+
| id  | name |
+-----+------+
|  1  | Elto |
|  2  | John |
+------------+

This is the conditions.
I generate the query in separated way. It's Just like :

$this->db->select('id, name');
$this->db->order_by('name', 'asc');
$q = $this->db->get('students');
$r = $q->result();

$r will produce:

[0] (name => 'Elto')
[1] (name => 'John')

Instead of that, I want the query produce something like this:

[0][0] => 'Elto'
[1][0] => 'John'

I don't know if this can be done. If anyone wants to help this problem, I would be very grateful.

0

4 Answers 4

2

You just need to use result_array() instead of result():

$this->db->select('id, name');
$this->db->order_by('name', 'asc');
$q = $this->db->get('students');
$r = $q->result_array();

result() will return you rows in Object format.

result_array() will return you rows into array format.

Please explore CodeIgniter Generating Query Results

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

2 Comments

Thank you for your response. This method works well, but does not produce what I wanted. result_array() will generate an associative array while indexed array is what I need. But it can be tricked with a php function: array_values(). But I do not know if there are any performance issues that may occur.
How counter intuitive for this answer to not provide the desired result, yet has been accepted. How confusing for researchers.
1

You can use

$r = $q->result_array();

or you can use

$r = $q->row_array(); 

To retrive only one item.

Check offical documentation for more information:

http://www.codeigniter.com/user_guide/database/results.html

Regards.

Comments

0

Codeigniter 4:

$r = $q->getResultArray();

Comments

0

To ensure that not only the first level contains indexed arrays, but also that each row contains indexed elements, apply a numeric alias to the name column in your SELECT statement and then use result_array() or getResultArray() (depending on your CI version).

Depending on your database driver, the alias quoting character may vary. Tested on my application running firebird, double quoting the numeric alias rendered properly.

return $this->db
    ->select(
        vsprintf(
            '%s %s, %s %s',
            $this->db->escape_identifiers(['id','0', 'name', '1'])
        ),
        false
    )
    ->order_by('name')
    ->get('students')
    ->result_array();

Will return an indexed array of single-element, indexed arrays.

[
    [0 => 1, 1 => 'Elto'],
    [0 => 2, 1 => 'John'],
]

Otherwise, you can iterate the row of the resultset and re-index them.

return array_map(
    'array_values', // or array_values(...)
    $this->db
        ->select('id, name')
        ->order_by('name')
        ->get('students')
        ->result_array()
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.