kalaba2003
kalaba2003

Reputation: 1229

Instead of foreach loop, how can I use in_array()?

In codeigniter, in my controller file, I fetch data from model. To validate the results I use foreach loop and there is no problem. But instead of using foreach I want to use in_array() to control if variable is in database result array or not . Here is code :

function _remap($method,$params = array()){ 

//in construct, I use $this->model
if (isset($params[0])) {

    $this->db->distinct('*****');
    $this->db->from('*****');
    $this->db->where('****',****);

    $query = $this->db->get();

    if (in_array($params[0],$query->results())) {
        echo "in_array works";
    }
}

But not echoing anything. How can I do it?Thanks.

Upvotes: 0

Views: 978

Answers (3)

greenLizard
greenLizard

Reputation: 2346

I would suggest to write a helper method, that does the job you want and returns True or False. Information on how to do it can be found here

Upvotes: 0

Bhavin Rana
Bhavin Rana

Reputation: 1582

The in_array is just to be used for only checking a value in the value-list
e.g. array("Mac", "NT", "Irix", "Linux");

in_array wont work for your situation, because it returns object array.

Upvotes: 1

dan-lee
dan-lee

Reputation: 14502

According to the manual you could use result_array(). Otherwise you will get back an object. Of course you cannot use in_array for an object.

Also according to the manual you could change your code to

if (in_array($params[0], $query->first_row('array')) {
  // ...

Upvotes: 2

Related Questions