Any idea why this won’t work? I m getting tags a user has set up and want to get other users’ ids that have same tags.
Controller:
$this->load->model('tag_model');
$data['usr_tags'] = $this->tag_model->get_home_tags($user_id);
$tags = $this->tag_model->get_home_tags($user_id);
$tag_array = $tags;
$data['tag_users'] = $this->tag_model->get_tag_users($tag_array);
Model:
function get_tag_users($tag_array)
{
//$tag = array('item1','item2','item3');
$tag = $tag_array;
$query_str = 'SELECT DISTINCT user_id FROM tags WHERE tag IN ("'. implode('","', $tag) .'")';
$query = $this->db->query($query_str);
if($query->num_rows() > 0) {
foreach($query->result_array() as $tag_users) {
$data[] = $tag_users;
}
return $data;
}else{
return false;
}
}
Error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: models/tag_model.php
Line Number: 20
Your
$tag_arraylooks like this:It’s actually an array of arrays, you can’t use
implodeon it. The “Array to string” conversion happens because each element of the array is an array, which PHP need to convert to a string in order to “implode” them.You can use
array_mapto get each tag from the array.If you don’t have PHP 5.3, you can do it this way: