Andrew Smith
Andrew Smith

Reputation: 310

PHP foreach statement produces no output

the following produces no output, when really it should show a list including Milk, Cheese and Yoghurt. It is probably something really simple, I just can't see it.

 <?php 

 $FoodList=array();
 $newArray =array();
 echo "<p>";


 $Dairy= array(
    'a' => 'Milk',
    'b' => 'Cheese',
    'c' => 'Yoghurt',
 );
 $Fruit = array(
    'd' => 'Apples',
    'e' => 'Oranges',
    'f' => 'Grapefruit',
 );
 $GlutenFree = array(
    'g' => 'GF Cookies',
    'h' => 'GF Pancakes',
    'i' => 'GF Bread',
 );
 $Sweets = array(
    'j' => 'Musk Sticks',
    'k' => 'Caramels',
    'l' => 'Chocolate',
 );


 if ($_POST['running'] == 'yes')
 {
    $newArray = array_merge($FoodList, $Dairy);

    foreach ($newArray as $key => $value)
    {
       echo $value;
    }
 }


 echo "<p>";
 ?>

This may because the FoodList Array does not have anything in it, so I will look into that, but I have a strong feeling it is related to something else.

Upvotes: 0

Views: 85

Answers (2)

khan
khan

Reputation: 51

foreach($Dairy as $key => $value) {

echo $value; }

Upvotes: 0

Anthony Nandaa
Anthony Nandaa

Reputation: 3400

Your "bug" must be coming from the line, the array merge is fine:

 if ($_POST['running'] == 'yes')

Upvotes: 1

Related Questions