Array Multidimensional PHP foreach [duplicate]

0

I would like to know how to do a model search of a car using foreach, given the table below:

 /*
Carro                       modelo                      ano

toyota          "corrola","allion","Ranx","mark"        2008        
Mazda           "Atenza","Demio","MPV"                  2000

*/
    
asked by anonymous 01.09.2017 / 15:17

1 answer

0

I think this makes it easier to maintain:

<?php
$carros["toyota"]["modelos"] = array("corrola","allion","Ranx","mark");
$carros["Mazda"]["modelos"] = array("Atenza","Demio","MPV");

$carros["toyota"]["ano"] = 2008;
$carros["Mazda"]["ano"] = 2008;

foreach($carros as $carro) {
    foreach($carro["modelos"] as $modelo) {
        if($modelo == "Demio"){
            echo "Modelo " . $modelo . " Encontrado";
            break;
        }
    }
}
    
01.09.2017 / 15:40