0

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.

3
  • Does .get() return an object or array? Commented Jun 23, 2012 at 10:56
  • It returns query result as an array of objects. Use result_array() to return a pure array and then try using in_array Commented Jun 23, 2012 at 10:57
  • return object... Ohhh... In object I can not make it work, can I? Commented Jun 23, 2012 at 10:58

3 Answers 3

2

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')) {
  // ...
Sign up to request clarification or add additional context in comments.

2 Comments

I wonder why this was downvoted? Anyways, I wonder the parameter isn't just put the in query?
Yeah, I am wondering too. But you could help and make justice :)! I have no idea about codeigniter, I just used my general programming senses (and Google)
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.