?
Solved

Array for each question

Posted on 2014-03-04
4
Medium Priority
?
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 36

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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 111

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.
Suggested Courses

752 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