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();
}
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'];
}
result_array()
Returns the query result as a pure array. Typically you’ll use this in a foreach
loop.
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.
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" }