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 trialAkShay Mahajan
177 Pointscannot understand array_splice please explain
I tried to understand about array_splice but cannot. Explain the parameters required in array_splice.
3 Answers
Jon Maldia
8,473 PointsHi there, here's how I understand this.
array array_splice ( input , offset, length, replacement )
input - this is the array that you will be using to splice
offset - this is where you start to splice. If the number is positive, you start from the left. If it's negative, you start from the right.
length - if this is positive, this is how many values will be removed. if this is is negative, you count from the left up to the absolute number - that's where you stop removing. If it's 0, you don't remove anything, you basically add the replacement starting at the offset
replacement - if specified, this will be the value you will add to the array
Examples:
$input = array("apples", "oranges", "bananas", "cherries");
array_splice($input, 1);
// remove everything from $input[1] to the end of the array. The array is now array("apples")
$input = array("apples", "oranges", "bananas", "cherries");
array_splice($input, 1, -1);
// Remove everything from $input[1]. Then, you count 1 from the right ("cherries"). That's where you stop. You remove "oranges" and "bananas". The array is now array("apples", "cherries")
$input = array("apples", "oranges", "bananas", "cherries");
array_splice($input, -1, 1, array("watermelon", "mangoes"));
// Count from 1 from the right since the offset is negative. That's where you start. Then you remove 1 because length is one. You remove "cherries". Replace that with "watermelon" and "mangoes" from $input[3]. The array is now array("apples", "oranges", "bananas", "watermelon", "mangoes")
?>```
Jon Maldia
8,473 PointsSorry. Yea, that should be cherries and not bananas. I fixed it.
somya shrivastava
1,431 Pointsthank you Jon :)
AkShay Mahajan
177 PointsAkShay Mahajan
177 PointsIn third example I think you did a small mistake. You said that we have to remove bananas but you removed cherries from $input.
Count from 1 from the right since the offset is negative. That's where you start. Then you remove 1 because length is one. You remove "bananas". Replace that with "watermelon" and "mangoes" from $input[3]. The array is now array("apples", "oranges", "bananas", "watermelon", "mangoes")
Please correct me if I am wrong.
nico dev
20,364 Pointsnico dev
20,364 PointsNow that helped a lot.
Thank you!