Friday 19 October 2018

Codeigniter, result() vs. result_array()

Result has an optional $type parameter which decides what type of result is returned. By default ($type = "object"), it returns an object (result_object()). It can be set to "array", then it will return an array of result, that being equivalent of caling result_array(). The third version accepts a custom class to use as a result object.
The code from CodeIgniter:
public function result($type = 'object')
{
    if ($type === 'array')
    {
        return $this->result_array();
    }
    elseif ($type === 'object')
    {
        return $this->result_object();
    }
    else
    {
        return $this->custom_result_object($type);
    }
}
Arrays are technically faster, but they are not objects. It depends where do you want to use the result. Most of the time, arrays are sufficient.
Example:
// $query->result_object() === $query->result()
// returns:
Array ( [0] => stdClass Object ( [col_A] => val_1A , [col_B] => val_1B , ... ) 
        [0] => stdClass Object ( [col_A] => val_2A , [col_B] => val_2B , ... ) 
        ...  
      ) 

// $query->result_array() !== $query->result()
// returns:
Array ( [0] => Array ( [col_A] => val_1A , [col_B] => val_1B , ... ) 
        [1] => Array ( [col_A] => val_2A , [col_B] => val_2B , ... )
        ... 
      )
Disediakan oleh : Che Wahida Binti Che Pauzur
Sumber : https://stackoverflow.com/questions/16506789/codeigniter-result-vs-result-array

0 comments:

Post a Comment