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>

 

Link to comment
Share on other sites

<?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');
Edited by _EmilsM
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.