Populate 2 drops from Multidimentional Array

Hi,

I know it is possible but I am having difficulties trying to find help online for Multidimensional Arrays populating multiple drop-down menus.

I have 3 dropdown menus. The first has a list of car makes. The other two are for car model and car year. I would like to have the car make populate based on the car make selection and have the car year populate based on the car model selection. I do not want to use a database as the webhost has restirctions.

Here is a short snippet and the array looks something like this:

[php]$arr = array(
array(
“Make” => “Honda”,
“Model” => “Accord III Aerodeck”,
“Year” => 1988),
array(
“Make” => “Honda”,
“Model” => “Accord III Aerodeck”,
“Year” => 1989),
array(
“Make” => “Honda”,
“Model” => “Accord IV Aerodeck”,
“Year” => 1992),
array(
“Make” => “Honda”,
“Model” => “Accord IV Sedan”,
“Year” => 1991),
array(
“Make” => “Honda”,
“Model” => “Accord IX Sedan”,
“Year” => 2016)
);[/php]

Can anyone help with setting up a loop to pull out Model based on Make and Year based on Model.

Thanks for any advice.

This gets you what you are after, but there are better and cleaner ways to do so.

[php]$models = [];
foreach ($arr as $r) {
array_push($models, $r[‘Model’]);
}
$filtered_model = array_unique($models);
print_r($filtered_model);[/php]

Where does the array come from? The more correct way, would be to create a car class. Make, Model, and Year would be properties of the class. Then you would create an array of objects (Cars) that you would pull values from.

Sponsor our Newsletter | Privacy Policy | Terms of Service