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();
}
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'];
}
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" }
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()
return $this->db->get('users')->result();
/*
[
(object) ['id' => 1, 'name' => 'Abe'],
(object) ['id' => 2, 'name' => 'Bud'],
(object) ['id' => 3, 'name' => 'Cid'],
]
*/
return $this->db->get_where('users', ['id' => 4])->result();
/*
[]
*/
result_array()
return $this->db->get('users')->result_array();
/*
[
['id' => 1, 'name' => 'Abe'],
['id' => 2, 'name' => 'Bud'],
['id' => 3, 'name' => 'Cid'],
]
*/
return $this->db->get_where('users', ['id' => 4])->result_array();
/*
[]
*/
row()
return $this->db->get_where('users', ['id' => 2], 1)->row();
/*
(object) ['id' => 2, 'name' => 'Bud']
*/
return $this->db->get_where('users', ['id' => 4], 1)->row();
/*
null
*/
row_array()
return $this->db->get_where('users', ['id' => 2], 1)->row_array();
/*
['id' => 2, 'name' => 'Bud']
*/
return $this->db->get_where('users', ['id' => 4], 1)->row_array();
/*
null
*/
row('id')
return $this->db->get_where('users', ['id' => 2], 1)->row('id');
/*
2
*/
return $this->db->get_where('users', ['id' => 4], 1)->row('id');
/*
null
*/
row_array('id')
return $this->db->get_where('users', ['id' => 2], 1)->row_array('id');
/*
2
*/
return $this->db->get_where('users', ['id' => 4], 1)->row_array('id');
/*
null
*/