Putting it all together (Advanced Arrays)


#1

Hey all! Trying to wrap up this class, but its keeps telling me that I haven't printed out all my items but I have. Any help would be appreciated!!! thanks in advance:

<?php
// On the line below, create your own associative array:
$myArray = array('comic' => 'Descender', 'movie' => 'Bad Boys 2');

  // On the line below, output one of the values to the page:
    echo $myArray['comic'];
      
  // On the line below, loop through the array and output
  // *all* of the values to the page:
    echo '<br/><br/>i love to injest media, here are some favs: <br/>';
    foreach($myArray as $mediaType => $title){
        echo $title . ' is a really great ' . $mediaType . '.<br/>';
    }
  ?>

#2

Maybe it wants you to "print" ?? (this tool can be very specific, IDK...)

I personally would just use the double quotes (that expand variables within a String) and forget about all the dots

<?php
// On the line below, create your own associative array:
$myArray = array('comic' => 'Descender', 'movie' => 'Bad Boys 2');

  // On the line below, output one of the values to the page:
    echo $myArray['comic'];
      
  // On the line below, loop through the array and output
  // *all* of the values to the page:
    echo '<br/><br/>i love to injest media, here are some favs: <br/>';
    foreach($myArray as $mediaType => $title){
        print "$title is a really great $mediaType <br/>";
    }
  ?>

Technically it looks correct, unless it specifically wants "print" instead of "echo" in other words.

I can't see the instructions, so I don't know how many items it was expecting in the array either...and sometimes the environment just doesn't work...copy code, then reset code, reload page, and paste again, and see what happens...


#3

Not sure this helps, but my code was rejected by the SCT because of white space.

This passes,

echo "<br>$city=&gt;$team";

while this does not,

echo "<br>$city =&gt; $team";

This is the associative array:

$myArray = array(
    'YVR' => 'Canucks',
    'YYC' => 'Flames',
    'YEG' => 'Oilers'
);

#4

Thank you both....neither fixed the issue....this is bugging me, hmmm, i updated to the below and added the instructions:

Instructions:
Create a variable called $myArray and set it equal to a new array that you create, which can contain any items you like.
Grab a value from your array and display it.
Now loop through the array and output the contents to the browser!

My Code:

  <?php
  // On the line below, create your own associative array:
    $myArray = array('comic'=>'Descender', 'movie'=>'Bad Boys 2');

  // On the line below, output one of the values to the page:
    echo $myArray['comic'];

  // On the line below, loop through the array and output
  // *all* of the values to the page:
    echo '<br/><br/>i love to injest media, here are some favs: <br/>';
    foreach($myArray as $mediaType=>$title){
        print "$title is a really great $mediaType <br/>";
    }
  ?>

#5

After all kinds of testing I've finally concluded that the values may not contain white space for this exercise. Change "Bad Boys 2" to something like "Superman" and it will pass.

<p>
      <?php
  // On the line below, create your own associative array:
    $myArray = array(
        "comic"=>"Descender", 
        "movie"=>"Batman", 
        "game" =>"SuperMario"
    );

  // On the line below, output one of the values to the page:
    echo $myArray['comic'];

  // On the line below, loop through the array and output
  // *all* of the values to the page:
    foreach($myArray as $mediaType => $title){
        echo "<br>$mediaType=&gt;$title";
    }
  ?>
    </p>

#6

Thank you for the help!!!! seems like once i put 3 items in my Associative array it went through. Weird