This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Arrays Forum

View Course » View Exercise

769 points
52d9a5f47c82caf1eb0000a5_631805601
Submitted by
Pedro Morais
over 4 years ago

why is my array wrong?

  <?php
  $friends = array ('Jules', 'Vincent Vega', 'Butch');
  ?>

error msg: Oops, try again! Did you remember to put at least three elements in your array? Make sure each one is a string in quotes!

if i do it like this:

  <?php
  $friends = array ('Jules', 'Vincent Vega', 'Butch');
  print_r ($friends);
  ?>

it continues with the same error message but the array is printed in screen:

Array ( [0] = Jules [1] = Vincent Vega [2] = Butch )

the result is the same with (' ') or (" ") in the strings.


this exercise worked when i inserted "do", "re", "mi" as friends. it didn't allow other names.


6 votes

permalink

The reason why the first one didn't worked was because you had space between Vincent Vega. You're only allowed to have 1 name without space. :P

379 points
B09738f202544b1242d89eeb6a15f5d3?s=140&d=retro
Submitted by
Laampan
over 4 years ago

2 Comments

52d9a5f47c82caf1eb0000a5_631805601 Pedro Morais over 4 years ago

yep, that's it! but shouldn't arrays alow strings with more than one word?

19ff313aa58e7f3524926d04a65ad91e?s=140&d=retro Curran over 4 years ago

Arrays do allow strings with more than one word, it is just the exercise that is giving the error. What you put is valid PHP, just not in this exercise.


3 votes

permalink

Yeah, you have have spaces in the names.

Wont work : " $friends = array( "This", "Will Not", "Work" ); "
BECAUSE THERE IS A SPACE.

Will Work : " $friends = array( "This", "Will", "Work" ); "
BECAUSE THERE IS NO SPACES IN BETWEEN THE WORDS / NAMES.

745 points
526f40a2f10c60fd60000ba7_599031487
Submitted by
SirenNinja
over 4 years ago

3 Comments

52d9a5f47c82caf1eb0000a5_631805601 Pedro Morais over 4 years ago

yep, that's it! but shouldn't arrays alow strings with more than one word?

526f40a2f10c60fd60000ba7_599031487 SirenNinja over 4 years ago

1 word per 2 quotation marks. That's how you create more than 1 word. It should be " More That 1 Word" but, sadly it's not.

522ecc4080ff33208d001629_653207159 hao over 4 years ago

i got same error msg, thank you, i understand.


3 votes

permalink

For those who can't understand why their code does not pass: the exercise will only allow you to go on if you have exactly 3 elements in your array.

In the error reason output, "at least" suggests that you could put more than 3 elements in your array, that shoud be corrected.

930 points
Picture
Submitted by
Akian
over 4 years ago

2 Comments

F3bf63e3a698f2e10850a1223ffccb26?s=140&d=retro jlancasterwashcald.com over 3 years ago

That's it.

06a25abd3d5e2a79e671b8e674195518?s=140&d=retro qbert007 over 2 years ago

That was an infuriating 15 minutes, because I had one extra name in the string, so there were "at least" three. Instead of "three".


2 votes

permalink

Same problem. Only one word per quote, just first names. Tried saving and refreshing. No luck. This PHP track seems rife with errors.

  <?php
    $friends = array("Justin", "Dan", "Luke", "Brian");
  ?>

738 points
Picture
Submitted by
David Karasek
over 4 years ago

5 Comments

526f40a2f10c60fd60000ba7_599031487 SirenNinja over 4 years ago

The track only asked for 3 of your friends names...

Picture David Karasek over 4 years ago

Yeah, for some reason I thought it said 3 or more. But, if that is the error, couldn't it output that as the reason?

526f40a2f10c60fd60000ba7_599031487 SirenNinja over 4 years ago

It does. " Did you remember to put at least three elements in your array? "

It tell's you that you need to put 3. No more or less.

52159b0f80ff33805a001770_641392019 Anarcho Nomad about 4 years ago

"at least" means that many or more. It should say "put exactly three elements."

526f40a2f10c60fd60000ba7_599031487 SirenNinja over 3 years ago

Well, true. I could be wrong, but I only used 3.


1 vote

permalink

For me I was sure my code was right but it wouldn't pass. Just saving and refreshing the page made it work, some of you may want to try that :)

578 points
589d0a9c854be946660000e0_814875114
Submitted by
David Munday
over 4 years ago


1 vote

permalink

This is the answer.

My First Array



<?php

$friends = array("do", "re", "mi");
?>

212 points
E20705acb37bb9ad0ff4d143518083dc?s=140&d=retro
Submitted by
patel17
over 4 years ago


0 votes

permalink

Mine worked only with

<?php
$friends = array("do", "re" , "mi" );
?>

using spaces between the strings and the comas.

1461 points
52c816e49c4e9d8f54000e7d_949788089
Submitted by
Tiago
about 3 years ago


0 votes

permalink

I had the same space problem. Clearing the space makes it work.

Bad code:
<?php
$friends = array("do", "re", "The Fonze");
?>

Good code:

<?php
$friends = array("do", "re", "TheFonze");
?>

1008 points
A4198b29f8d602e9b5091ce753039687?s=140&d=retro
Submitted by
Joseph Finch
almost 3 years ago