gazrobur gazrobur - 3 months ago 14
PHP Question

How to merge array with result in Codeigniter?

I have this code in Codeigniter, but somehow array_merge does not want to work, how is it possible to merge array to the result? please help me, Thanks.

$categories = $this->get_categories();

$data = array();

foreach ($categories as $index) {
$this->db->select('*');
$this->db->from('questions');
$this->db->where('category', $index);
$this->db->order_by('id','RANDOM');
$this->db->limit(3);
$query = $this->db->get();

array_merge_recursive($data, $query->result());


}

return $data;

Answer

You need to save the result of array_merge_recursive() to the $data string:

$data = array_merge_recursive($data, $query->result());
Comments