I have an array of objects, where each object contains an array for an attribute
$cars = [
"toyota" => ["colors" => ["red","blue"]],
"mazda" => ["colors" => ["red"]],
"honda" => ["colors" => ["blue"]],
"nissan" => ["colors" => ["red","yellow"]],
];
I need a function that will return all the cars that have a specified color.
I can do this using a foreach loop.
function getCarsByColor(string $color, array $cars){
foreach($cars as $key => $car){
if(!in_array($color, $car['colors'])){
unset($cars[$key]);
}
}
return $cars;
}
Just wondering if there's a more direct way of doing this.
Thanks.
Select car.name, color.name from car inner join color on color.idCar = car.id where color = 'yourColorByPreparedStatementHere'