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

array_slice

<?php $input = array("a", "b", "c", "d", "e"); $output = array_slice($input, -2, 1); // returns "d" ?>

i am not getting how this function work ?

1 Answer

array_slice() returns all or part of an array as a new array. There's a good explanation at w3schools: http://www.w3schools.com/php/func_array_slice.asp

In particular, though, the statement $output = array_slice($input, -2, 1); returns "d" because the slice starts 2 from the end (the -1 start value in the 2nd parameter), and returns 1 element (the 1 length value in the 3rd parameter). So $output will be an array with one element.