Why does the tutorial say that something is not printing to the page when it is being printed to the page?


#1



https://www.codecademy.com/courses/web-beginner-en-QnQrd/0/7


My code works correctly, but the tutorial gives me an error. The code is printing every entry of the associative array. What can I do to get past what it thinks is an error? The error message is below:

Oops, try again. It looks like '1=>Martin' is part of $myArray, but it wasn't printed to the page!

The output of the code is:
Martin
Jeter
Player 1 is Martin
Player 2 is Jeter
Player 3 is Ruth
Player 4 is Gehrig
Player 5 is DiMaggio


      $myArray = array(1 => "Martin", 2 => "Jeter", 3 => "Ruth", 4 => "Gehrig", 5 => "DiMaggio");

      // On the line below, output one of the values to the page:
       echo $myArray[1] . "<br>";
       echo $myArray[2] . "<br>";
          
      // On the line below, loop through the array and output
      // *all* of the values to the page:
   foreach ($myArray as $num =>$name) {
      echo "Player " . $num . " is " . $name . "<br>";
}


#2

Try printing out only one array item, instead of two. Then try with the key in quotes,

    echo $myArray['1'];

#3

Thanks for the suggestions, but the problem is still there.
I printed just one array item. I added the quotes. Then, when those did not fix the issue, I put the quotes on each item in the array definition.
It still gives this error message.


#4

Copy your code and paste into notepad. Reset the lesson. Refresh the page.

Check your code for errors, and fix any that you find. Copy the code and paste it all in with a single action. Save and Submit.


#5

Thanks. I did that but it did not fix the problem. The output displays when I type the code in, but when I click the button to run it, then it gives the error, even though it does appear to print the entire array.

So, I changed the code to not use integers, see below, but it gives the same error. I just want to get past this and complete the course. Does anyone have something that works for this part? I'll keep playing with it tonight and post if something does work for this. I've had a lot of issues with this course where things did not run so I killed the browser, came back in, and pasted the same code and it worked. It's not working for this one though.
// On the line below, create your own associative array:
$myArray = array("Martin" => "One", "Jeter" => "Two", "Ruth" => "Three", "Gehrig" => "Four", "DiMaggio" => "Five");

  // On the line below, output one of the values to the page:
 echo $myArray["Jeter"] . "<br>";
      
  // On the line below, loop through the array and output
  // *all* of the values to the page:
 foreach ($myArray as $pname => $pnum) {
  print "Player " . $pname . " is " . $pnum . "<br>";

}


#6

Found it! Sorry, I didn't notice where there were other topics, but found that now. It seems to be a bug in the tutorial. If I change the assoc array to have one key pair per line it works. So I am finished with this course.
The fix is to put it in there as:
$myArray = array("Martin" => "One",
"Jeter" => "Two",
"Ruth" => "Three",
"Gehrig" => "Four",
"DiMaggio" => "Five");

Maybe include the next 4 uni numbers too, White, Mantle, Yogi & Maris!


#7

Barring any other quirks, or errors this would appear to be a workaround for anybody facing the same issue.

Could you please post your finished code so we can tinker with and test it to reproduce this issue. It helps if we understand the problem before we report it, and we'll need your code to do this. Thank you.


#8

This is what I have in my test bed for the lesson in the OP link.

<html>
  <head>
    <title>I am the King of Arrays!</title>
  </head>
  <body>
    <p>
      <?php
      // On the line below, create your own associative array:
      $myArray = array('Van'=>'Canucks','Calg'=>'Flames','Edm'=>'Oilers');

      // On the line below, output one of the values to the page:
      echo $myArray['Edm'];
          
      // On the line below, loop through the array and output
      // *all* of the values to the page:
      foreach ($myArray as $city => $team) {
          echo "<br>$city=&gt;$team";
      }
    ?>
    </p>
    <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>
    <p>
    <?php
    $myArray = array(1 => "Martin", 2 => "Jeter", 3 => "Ruth", 4 => "Gehrig", 5 => "DiMaggio");
      echo $myArray['1'] . '<br>';
  // On the line below, loop through the array and output
  // *all* of the values to the page:
 foreach ($myArray as $pname => $pnum) {
  print "Player " . $pname . " is " . $pnum . "<br>";

}
      ?>
      </p>
  </body>
</html>


It passes with no issues.


#9

This failed with the error mentioned above, even with resetting,
killing the browser, etc, and then retyping it.

// On the line below, create your own associative array:

$myArray = array("Martin" => "One", "Jeter" => "Two", "Ruth" =>
"Three", "Gehrig" => "Four", "DiMaggio" => "Five");

// On the line below, output one of the values to the page:
echo $myArray["Jeter"] . "
";

// On the line below, loop through the array and output
// all of the values to the page:
foreach ($myArray as $pname => $pnum) {
print "Player " . $pname . " is " . $pnum . "
";

}

It worked when I changed the array definition to:
$myArray = array("Martin" => "One",
"Jeter" => "Two",
"Ruth" => "Three",
"Gehrig" => "Four",
"DiMaggio" => "Five");


#10

I was just thinking that maybe this is a browser specific issue. I am running Google Chrome (it says Google Chrome.Ink on the Details tab) on an old Windows PC running Vista. It didn't work when I tried it at work either, and that was probably while using Chrome on a much more modern system.


#11

These lessons have an auto-refresh built-in so the code is live as we type. To disable this 'feature', paste the following above your working code:

<?php else ?>

It will force a parsing error that will disable the auto-refresh. Finish and check all your typing, Save and Submit (will fail), then refresh the page, remove that line and Save again.


#12

I'm having the same issue, but with a multi dimensional array.
Here's a view of what I'm seeing:

And my code

      <?php
      // On the line below, create your own associative array:
        $myArray = array(array("Ace of Spades",1),
                    array("2 of Diamonds",2),
                    array("3 of Hearts", 3));

      // On the line below, output one of the values to the page:
        echo $myArray[2][0]."<br />";
          
      // On the line below, loop through the array and output
      // *all* of the values to the page:
      $length =count($myArray);
      
      for($i=0; $i<$length; $i++){
         echo "You have a ".$myArray[$i][0]." in your hand!"."<br />";
      }
     
      ?>

Am I missing something?


#13

@djvanhandel Why your array $myArray looks like this (1 => "Martin") the other way around? Should it be ("1" => "Martin") or ("Martin" => 1)? I am not in touch with php for long time so check this with others too.


#14

It looks good to me. Try what mtf suggested above.
You can also try to put a newline after each comma in your array, like below, just to see if that works.
It looks sloppy, but it might get you past this.

$myArray = array(array("Ace of Spades",
1),
array("2 of Diamonds",
2),
array("3 of Hearts",
3));


#15

I have completed the php a long time ago in codecademy without any issues.


#16

@djvanhandel Thank you! The work around mtf suggested didn't work, but your suggestion of the newlines did. Appreciate the help :smile:


#17

You are welcome. Mashuk - Congrats on it working for you. Please let the site managers know which browser/version you used, etc, anything that can help them determine why so many people encountered this issue.


#18

Hey @djvanhandel I think your message suppose to be for @cmpisha


#19

It doesn't do anything except disable the auto-refresh. You have to remove it when you submit.


#20

Try with a single dimension array. The SCT won't know to use a 2-D index.