7 Array Functions II


#1

This is my code. It works perfectly. However, CodeAcademy won't let me pass the lesson. It returns this error:
Your sorted array should have at least 3 elements in it. Make sure to pass a comma as the first argument to join() !

<?php
// Create an array with several elements in it,
// then sort it and print the joined elements to the screen
$countries = array('Romania', 'UK', 'Spain', 'US', 'Russia');
sort($countries);
print join($countries);
?>



<?php
// Reverse sort your array and print the joined elements to the screen
$countries = array('Romania', 'UK', 'Spain', 'US', 'Russia');
rsort($countries);
print join($countries);
?>


#2

I believe that you code is missing a "glue" (join takes two agruments join(glue, $array)) and should look like this:

<?php
// Create an array with several elements in it,
// then sort it and print the joined elements to the screen
$countries = array('Romania', 'UK', 'Spain', 'US', 'Russia');
sort($countries);
print join(";", $countries);
?>

<?php
// Reverse sort your array and print the joined elements to the screen
$countries = array('Romania', 'UK', 'Spain', 'US', 'Russia');
rsort($countries);
print join(";", $countries);
?>

#3

Thanks for your reply. I added the ";" glue but it still gives me the same error message. My sorted array does have at least 3 elements in it, actually it has 5. :expressionless:


#4

I got it! It's supposed to be a comma.

print join (",", $countries);
?>


#5

It does not have to be comma, it's problem with codecademy website. you should be able to use any string as glue


#6

All right then, thanks Creative_Coder! :blush:


#7

Not the same problem but having difficulties with Array Functions II.

My code brings up this error:

Oops, try again. Did you remember to use array() in your code?
Here's my code:



<?php
// Create an array with several elements in it,
// then sort it and print the joined elements to the screen
$numbers = array(3, 15, 1, 4, 22, 8);
sort($numbers);
print join(",", $numbers);
?>



<?php
// Reverse sort your array and print the joined elements to the screen
$numbers = array(3, 15, 1, 4, 22, 8);
rsort($numbers);
print join(",", $numbers);
?>




#8

try this:
switching to single quotes got it to pass green for me.

print join(',', $numbers);


#9

Managed to get it to pass by creating the array first and then assigning elements to it after. I'm guessing the single quotes also would have worked


#11

I think you misunderstood her. She missed comma between the glue comma and the array.
i.e. before:
print join ("," $countries);
after:
print join (",", $countries);

that is common, I suppose, as the comma is missing in the task itself:
"03. Then, print out join("," $the_array)."


#12

supposed to be like this......



<?php
// Create an array with several elements in it,
// then sort it and print the joined elements to the screen
$countries = array('Romania', 'UK', 'Spain', 'US', 'Russia');
sort($countries);
print join(",", $countries);
?>



<?php
// Reverse sort your array and print the joined elements to the screen
$countries = array('Romania', 'UK', 'Spain', 'US', 'Russia');
rsort($countries);
print join(",", $countries);
?>