Jump to content

Echo value from one array, based on the result of conditional involving another array...


sonnyj7

Recommended Posts

Let's assume that there is an EMPTY "$flavors" array that has already been established...Here's the problem:

 

Make sure that foreach loop executes correctly if the $flavors array is NOT empty. Add one element to the $flavors array with a value of 'Avocado Chocolate'.

                 

 

                 (I can't seem to figure this out -THIS IS AN EXERCISE)

             

  Here's the STARTER code to the problem:

 

                  <?php

 
    
   
    $recommendations = array();
 
                               // This is the code that I added 
 
                                $flavors = array()
                                $flavors = array("Avocado Chocolate");
                              
 
 
 
?><html>
<body>
 
 
    <h1>Flavor Recommendations</h1>
    
<?php if (!empty($flavors)) {
    
      
   echo '<ul>
        <?php foreach ($recommendations as $flavor) { ?>
            <li><?php echo $flavor; ?></li>
        <?php } ?>
    </ul>' ;
    } else {
  echo '<p>There are no flavor recommendations for you.</p>';  
    
  }?>
 
</body>
</html>

 

<?php
$recommendations = Array("Avocado Chocolate");
 
if(!empty($recommendations)) {
     echo '<ul>';
     foreach($recommendations as $flavor) {
       echo '<li>'.$flavor.'</li>';
     }
     echo '</ul>'
}
?>
.. and you can add to the recommendations array if you want..

$recommendations[] = 'flavor';

or

$recommendations = Array('flavor1', 'flavor2', 'flavor3');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.