Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Consultas MySQL para array php multidimensional


Tente consultar todas as linhas em um JOIN e, em seguida, iterando para renderizar para multidimensional:
$query = "SELECT *, materials.id AS material_id FROM job_truck JOIN job_truck_material ON job_truck_material.job_truck = job_truck.id JOIN materials ON job_truck_material.material = job_truck_material.id";
$jobs = array();

foreach ($conn->query($query ) as $row) {
    $job_id = $row['jobid'];
    $truck_id = $row['truck_id'];
    if(empty($jobs[$job_id])) {
        $jobs[$job_id] = array();
    }
    if(empty($jobs[$job_id][$truck_id])) {
        $jobs[$job_id][$truck_id] = array();
    }
    $jobs[$job_id][$truck_id][$row['material_id']] = $row['material'];
}