How to solve array to string conversion error in codeigniter?
Answers
Explanation:
Here is my model:
public function count_diabetic(){
$query = $this->db->query("Select count(diabetic) as count_diabetic from member where diabetic is not NULL");
return $query->result_array();
}
public function count_hypertensive(){
$query = $this->db->query("Select count(hypertensive) as count_hypertensive from member where hypertensive is not NULL");
return $query->result();
}
here is my controller:
public function home(){
$this->load->model('Jsv_model');
$data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata($data);
$this->load->view('home');
}
Here is my view with in php tag:
echo $this->session->userdata('count_diabetic');
But Here it shows error that array to string conversion error..
The error "array to string conversion" in code igniter can be solved as follows:
$arr = array( );
for each($arr as $val)
{
echo $val, '<br>';
}
- This code will loop through the array.
- One more way to rectify this error is to implode the array into a comma-delimited string. This can be done in the following way:
$arr = array( );
echo implode(' , ', $arr) ;
- This error occurs when an array is treated like a string that cannot be done. So PHP throws this error.