Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a multidimensional array which I want to display in a foreach loop. I've been looking at lots of tutorials but I haven't been able to make it work yet.

This is my array and foreach loop:

$events = array( 
           array( Name => "First Event", 
                  Date => "12/13/14",
                  Time => "12:13"
                  Description => "event description"
                ),
           array( Name => "Second Event", 
                  Date => "12/13/14",
                  Time => "12:13",
                  Description => "event description"
                ),
           array( Name => "Third Event", 
                  Date => "12/13/14",
                  Time => "12:13"
                  Description => "event description"
                )
         );

foreach($events as $event) {
    echo "<div class=\"event\"><strong>";
    echo $event[Name];
    echo "</strong><em>";
    echo $event[Date] . " at " . $event[Time];
    echo "</em><div>";
    echo $event[Description];
    echo "</div></div>";
}

and here is how I want it to display:

<div class="event">
  <strong>Event Name</strong><em>Date at Time</em>
  <div>
    Description 
  </div>
</div>

I would appreciate any help you could give. Thanks!

share|improve this question
6  
Can you please post your attempt at the foreach? We are happy to help you debug your code, but we aren't going to write it for you. –  adear11 Jan 1 at 18:46
    
Basically, for each dim in your array, you nest a foreach loop. In your case, the first foreach gives you each array inside, and normally, you'd use an inner foreach on the array given by the first foreach to iterate over that array's contents. However, please see the answer below as it answers your problem specifically. –  Zarathuztra Jan 1 at 18:52
    
@adear11 done.. –  Nathan B Jan 1 at 19:06

3 Answers 3

up vote 2 down vote accepted

The keys are missing quotes and Time => "12:13" missing comma "," at the end:

<?php
$events = array( 
           array( "Name" => "First Event", 
                  "Date" => "12/13/14",
                  "Time" => "12:13",
                  "Description" => "event description"
                ),
           array( "Name" => "Second Event", 
                  "Date" => "12/13/14",
                  "Time" => "12:13",
                  "Description" => "event description"
                ),
           array( "Name" => "Third Event", 
                  "Date" => "12/13/14",
                  "Time" => "12:13",
                  "Description" => "event description"
                )
         );

foreach($events as $event) {
?>
<div class="event">
    <strong><?php echo $event["Name"];?></strong><em><?php echo $event["Date"];?></em>
    <div><?php echo $event["Description"];?></div>
</div>
<?php
}
?>

Output

First Event 12/13/14

event description

Second Event 12/13/14

event description

Third Event 12/13/14

event description

share|improve this answer

The keys should be in quotes. E.g.: 'Name' rather than Name

<?php foreach($events as $event): ?>
<div class="event">
   <strong><?php echo $event['Name'] ?></strong><em><?php echo $event['Date'] ?> at <?php echo $event['Time'] ?></em>
   <div>
    <?php echo $event['Description'] ?>
  </div>
</div>
<?php endforeach; ?>
share|improve this answer
    
Yay thanks guys--I can finally post comments to questions now that I have > 50 points! –  user24601 Jan 1 at 18:59

Try this:

foreach($events as $record) {

    $name = $record["name"];
    $date = $record["date"];
    $time = $record["time"];
    $description = $record["description"];

    print("
      // your html code with the variables here
    ");

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.