Search code examples
phparray-merge

What the best way to deal with codeigniter php array_merge, in a foreach?


What is the best way to deal with php array_merge, in a foreach


Solution

  • $school_students is being overwritten for every pass of your foreach-loop. One alternative is to simply add the results to an array like so:

        foreach($schools as $row):
                $school_db_name    =   $row['school_db_name'];
                $this->db->query("use $school_db_name");
                $school_students[]    =   $this->db->select('*')
                                        ->from('school_students')
                                        ->get()->result_array();
        endforeach;
    

    If you need to flatten the array afterwards you can for example use this:

    $recipients = array_merge(...$school_students)