Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Datatypes PHP Datatypes Challenge

Seth Warner
Seth Warner
5,348 Points

How would you echo the second value in the array?

<?php

//Place your code below this comment $integer_one = 1; $integer_two = 2; $golden = 1.618; $bool = true; $colors = array('red', 'blue', 'green'); echo $colors['blue']; print_r($colors);

?>

index.php
<?php

//Place your code below this comment
$integer_one = 1;
$integer_two = 2;
$golden = 1.618;
$bool = true;
$colors = array('red', 'blue', 'green');
echo $colors['blue'];
print_r($colors);

?>

3 Answers

Arrays are 0 indexed, so if you echo $array ( [0] ); will echo the first value. So, echoing the second value requires ......

Ted,

Not going to edit your post, but to access an array, you'd use the brackets [ ] not the parentheses. you would thereby use echo $array[0] for the first value in the array.

Cheers!

Of course you are right. That was my first instinct but I second guessed myself. Edited to reflect it correctly. I also learned that {} are interchangeable with []. http://php.net/manual/en/language.types.array.php

Seth,

Go with what Ted has stated but remember, that is if you are using the default numbered indexes like it is using in the example. You can also name the keys within the array. If you do that, you'd reference the name you used when creating the array:

<?php

$myarray = array('key1' => 'val1');
echo $myarray['key1'];

?>

Cheers!

Seth Warner
Seth Warner
5,348 Points

awesome! thank you guys for your help, sorry I couldnt get back sooner!