0

I am trying to use the PHP function in_array within a view using CodeIgneter.

This is the method

class User_details extends CI_Model {

public function checkID(){
$query = $this->db->query("SELECT id FROM users");

return $query->result_array();

}

This is the controller

$this->load->model('user_details');

$data['allUserID'] = $this->user_details->checkID();
$this->load->view('user_details/content',$data);

This is the script I am trying to use in the view

$id = $this->uri->segment(4);//user's id for example: 218


if(in_array($id,$allUserID)){
echo 'exists';
} else {
echo 'does not exist';
}

Unfortunately he script does not work as expected.

If I check the array using print_r($allUserID) I get following result:

Array ( [0] => Array ( [id] => 217 ) [1] => Array ( [id] => 218 ...
...and so on...

Last week I started learning CodeIgneter and I did not understand its approach yet.

1
  • 1
    there is nothing related to codeigneter and in_array its a php function used to find something in array. you are using it in a wrong way. it is used for single dimensional array Commented Apr 1, 2015 at 13:47

1 Answer 1

2

in_array() accept single dimensional array and you are passing two dimensional array. Try converting it to single dimensional array like

$array = array_map('current', $allUserID);

if(in_array($id, $array)){
 echo 'exists';
} else {
  echo 'does not exist';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to all of you. Yes, sure: I was using in_array() in a wrong way.

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.