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 trialSeth Warner
5,348 PointsHow 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);
?>
<?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
Ted Sumner
Courses Plus Student 17,967 PointsArrays are 0 indexed, so if you echo $array ( [0] ); will echo the first value. So, echoing the second value requires ......
Shawn Gregory
Courses Plus Student 40,672 PointsSeth,
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
5,348 Pointsawesome! thank you guys for your help, sorry I couldnt get back sooner!
Shawn Gregory
Courses Plus Student 40,672 PointsShawn Gregory
Courses Plus Student 40,672 PointsTed,
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!
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 PointsOf 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