I have a query in mysql that returns a row of records like this, the first field is the marca
and the second the modelo
, what I try to do with PHP is to mount an array with the following structure to pass it in view of blade grouped by brand and vehicle.
Sample SQL results
'FORD','FOCUS',1,2,3
'FORD','FOCUS',2,2,3
'FORD','FOCUS',3,1,2
'FORD','TRANSIT',1,2,3
'FORD','TRANSIT',2,2,3
'FORD','GLOBO',1,2,3
The array I want to mount is type this
$coches = [
'ford' => [
focus => [
valor => 1,
valor => 2,
]
TRANSIT => [
valor => 1,
valor => 2,
]
]
]
On the other hand, the array that I have if I do a "dump" is kind of this
array:1 [▼
0 => array:1 [▼
"FORD" => {#205 ▼
+"code": "FOCUS"
+"title": "TEST"
}
]
]
The code that I have in PHP is something like this
$sql = "SELECT xxx ... ...";
$results = DB::select( DB::raw($sql), ['id' => $id]);
$newCar = array();
$newCars = array();
foreach ($results as $cars => $car)
{
if (empty($newCars)) {
$newCar[$course->code] = $course;
array_push($newCourses,$newCourse);
}
if (!array_key_exists('FORD',$newCourses)){
echo '-> '. $course->code.'<br>';
}
}
The last if
, does not fit anymore.
The logic that I try to do is create an array by car and model to pass it to the orderly view.