Solved

Array for each question

Posted on 2014-03-04
4
299 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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 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