Solved

Array for each question

Posted on 2014-03-04
4
281 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 82

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

708 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

17 Experts available now in Live!

Get 1:1 Help Now