9

What is the difference between row_array() and result_array()?

How would they be displayed on a view page?

if ($variable) {
    return $result->row_array();
} else {
    return $result->result_array();
}

4 Answers 4

16

From the documentation, row_array returns a single result and result_array returns multiple results (usually for use in a loop).

Examples from the documentation:

Result_array:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}

Row_array:

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   $row = $query->row_array(); 

   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}
Sign up to request clarification or add additional context in comments.

Comments

2
  1. result_array()

    Returns the query result as a pure array. Typically you’ll use this in a foreach loop.

  2. row_array()

    Returns a single result row. If your query has more than one row, it returns only the first row.
    Identical to the row() method, except it returns an array.

Comments

0

1) result_array(): return multidimensional array.

2) row_array(): return one-dimensional associative array

So, if you display structured information of each of them you will get something similar to the following:

echo var_dump(result_array());

Output:

array(1) { [0]=> array(4) { ["id"]=> string(1) "1" ["title"]=> string(12) "News title 1" ["slug"]=> string(5) "slug1" ["text"]=> string(57) "In the name of Allah, This is the first news description" } }

echo var_dump(row_array());

Output:

array(4) { ["id"]=> string(1) "1" ["title"]=> string(12) "News title 1" ["slug"]=> string(5) "slug1" ["text"]=> string(10) "description" }

Comments

0
  • result() returns an array containing zero or more objects.
  • result_array() returns an array containing zero or more arrays.
  • row() returns null if there are no results otherwise a flat object.
  • row_array() returns null if there are no results otherwise a flat array.
  • row(column_name) returns null if there are no results or no column by that name in the result, otherwise the scalar value in the first row.
  • row_array(column_name) returns null if there are no results or no column by that name in the result, otherwise the scalar value in the first row.

When an integer is passed to row() or row_array() the record at the corresponding index will be returned.


Scenario:

CREATE TABLE users (id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (id));
INSERT INTO users(name) VALUES ('Abe'),('Bud'),('Cid');
  • result()

    • with qualifying rows:
      return $this->db->get('users')->result();
      /*
      [
          (object) ['id' => 1, 'name' => 'Abe'],
          (object) ['id' => 2, 'name' => 'Bud'],
          (object) ['id' => 3, 'name' => 'Cid'],
      ]
      */
      
    • with no qualifying rows:
      return $this->db->get_where('users', ['id' => 4])->result();
      /*
      []
      */
      
  • result_array()

    • with qualifying rows:
      return $this->db->get('users')->result_array();
      /*
      [
          ['id' => 1, 'name' => 'Abe'],
          ['id' => 2, 'name' => 'Bud'],
          ['id' => 3, 'name' => 'Cid'],
      ]
      */
      
    • with no qualifying rows:
      return $this->db->get_where('users', ['id' => 4])->result_array();
      /*
      []
      */
      
  • row()

    • with qualifying rows:
      return $this->db->get_where('users', ['id' => 2], 1)->row();
      /*
      (object) ['id' => 2, 'name' => 'Bud']
      */
      
    • with no qualifying rows:
      return $this->db->get_where('users', ['id' => 4], 1)->row();
      /*
      null
      */
      
  • row_array()

    • with qualifying rows:
      return $this->db->get_where('users', ['id' => 2], 1)->row_array();
      /*
      ['id' => 2, 'name' => 'Bud']
      */
      
    • with no qualifying rows:
      return $this->db->get_where('users', ['id' => 4], 1)->row_array();
      /*
      null
      */
      
  • row('id')

    • with qualifying rows:
      return $this->db->get_where('users', ['id' => 2], 1)->row('id');
      /*
      2
      */
      
    • with no qualifying rows:
      return $this->db->get_where('users', ['id' => 4], 1)->row('id');
      /*
      null
      */
      
  • row_array('id')

    • with qualifying rows:
      return $this->db->get_where('users', ['id' => 2], 1)->row_array('id');
      /*
      2
      */
      
    • with no qualifying rows:
      return $this->db->get_where('users', ['id' => 4], 1)->row_array('id');
      /*
      null
      */
      

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.