PHP foreach Multidimensional Array

Asked

Viewed 2,367 times

0

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

 /*
Carro                       modelo                      ano

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

*/
  • 1

    Can you put how this table is structured in PHP? The car is the index?

1 answer

0

I believe 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;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.