Solved

Array for each question

Posted on 2014-03-04
4
289 Views
Last Modified: 2014-03-05
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'];
}
0
Comment
Question by:movieprodw
4 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 39904661
<?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

0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39904725
"make"  => array("bmw", "bmw", "bmw", "bmw", "bmw"), will create a numbered array starting with 0.  You could access it with a 'for' loop.
for ($i = 0; $i <= 5; $i++) {
    echo $hemdetails['make'][$i].' '.$hemdetails['model'][$i].' '.$hemdetails['minyear'][$i].' '.$hemdetails['maxyear'][$i];
}
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 39904793
Perfect
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39906184
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now