Avatar of movieprodw
movieprodw
 asked on

Array for each question

Hello,

I am confused.

I made the following array and thought it would 'foreach' like I wrote it, but obviously I am very wrong.

Can you please help me fix it.

$hemdetails = array (
    "make"  => array("bmw", "bmw", "bmw", "bmw", "bmw"),
    "model"  => array("1600", "1602", "2002", "2002ti", "2002tii"),
    "minyear" => array("1960", "1960", "1960", "1960", "1960"),
    "maxyear"   => array("1980", "1980", "1980", "1980", "1980" )
);

$hemcollection[] = '';

foreach($hemdetails as $details){
      echo $details['make'].' '.$details['model'].' '.$details['minyear'].' '.$details['maxyear'];
}
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Kimputer

<?php $hemdetails = array( array( make => "bmw", 
                      model => "1600",
                      minyear => "1960",
					  maxyear => "1980"
                    ),
               array( make => "bmw", 
                      model => "1602",
                      minyear => "1960",
					  maxyear => "1980"
                    ),
               array( make => "bmw", 
                      model => "2002",
                      minyear => "1960",
					  maxyear => "1980"
                    ),
				array( make => "bmw", 
                      model => "2002ti",
                      minyear => "1960",
					  maxyear => "1980"
                    ),
				array( make => "bmw", 
                      model => "2002tii",
                      minyear => "1960",
					  maxyear => "1980"
                    )
             );

$hemcollection[] = '';

foreach($hemdetails as $details){
      echo $details['make'].' '.$details['model'].' '.$details['minyear'].' '.$details['maxyear'];
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
movieprodw

ASKER
Perfect
Ray Paseur

You might want to learn about the idea of an array of objects.  You have all the same capabilities that you get with an array of arrays, but the syntax is easier to get right since you don't have all the fiddly quote marks.  And if you write your own class definitions you can have your own programming automatically applied to your data as you create or modify the objects.  Just a thought... ~Ray
Your help has saved me hundreds of hours of internet surfing.
fblack61