0

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?

$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;

1 Answer 1

1

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

$data = array_merge_recursive($data, $query->result());
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I've already tried but, is it a must to save into $data, can it be different variable? ex.: $res?
Yes you can, I try to take into account how readable the code is overall, i.e is $data the best variable name for the array of results? or would something like $categoryQuestions make more sense a year from now.

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.