The difference between row_array and result_array

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

How will they be displayed on the watch page?

 if ($variable) { return $result->row_array(); } else { return $result->result_array(); } 
+6
source share
3 answers

From the documentation, row_array returns one result, and result_array returns several 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']; } 
+14
source
  • result_array()

    Returns the result of the query as a clean array. Usually you use this in a foreach .

  • row_array()

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

+1
source

1) result_array (): return a multidimensional array.

2) row_array (): returns a one-dimensional associative array

So, if you display structured information about 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 name 1" ["slug"] => string (5) "slug1" ["text"] => string (57) "In the name of Allah, this is the first description of the news"}}

 echo var_dump(row_array()); 

Output:

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

0
source

All Articles