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 Arrays and Control Structures PHP Arrays Editing Array Elements

nico dev
nico dev
20,364 Points

Would array_splice take different separate keys as arguments? Otherwise, what would it be used?

Hi Community,

I was checking the arrays functions in the Teacher's Notes and playing with them (or at least trying!). The one I particularly liked was array_splice, but here's my situation.

I am trying with an array that has different months, and in between some other, completely unrelated, values, like this:

<?php

$test = array('Football', 'February', 'March', 'July', 'Tiniebla');

array_splice($test, 0, 'January', 3, 0, array('April', 'May', 'June'), -1, 0, 'August');
var_dump($test);

?>

As you can see, what I am trying to do above, is to do two things in one, removing the 'noise' from the array, while also adding (in order) the months that were not there.

Now, the problem I am having is that when I do the php command for that file, the console throws me an error, telling me that there are too many arguments, that array_splice would only take up to four (which, I guess, stands for the array, only one offset, only one length, and only one replacement, even if the latter can be an array, too).

Can't I pass arguments for different parts of the array to the array_splice function? Should I declare the whole array again? Or maybe use array_splice twice or thrice (once for each part, like January, April-May-June, August, etc.)?

Is there another function that should be used to do that?

Cannot find such an example or explanation in the php docs. Thank you in advance for your insight!

1 Answer

Hi Nico,

As you discovered, array_splice can only take 4 arguments so there is no way that I can think of to make all 3 changes that you need with 1 call to array_splice.

If all your bad data was at the end for example, and you only needed to add months at the end then you could do it with 1 call.

So you could probably do it with 3 calls to array_splice. Two of them would be to replace the bad data at the ends and the third to replace the missing months between March and July.

There's several different ways you could solve this problem.

Try taking a look at the array functions here: http://php.net/manual/en/ref.array.php

In particular, the merge, slice, and replace functions. You may find those useful in coming up with alternative solutions.

For example, you could use array_replace to replace your 2 bad values with January and August and then use array_splice to insert the missing months.

<?php

$test = array('Football', 'February', 'March', 'July', 'Tiniebla');
$result = array_replace($test, array(0 => "January", 4 => "August"));
array_splice($result, 3, 0, array('April', 'May', 'June'));

echo "<pre>";
var_dump($result);
echo "</pre>";

Experiment with the different functions and you'll likely find a few different ways to solve this problem.

nico dev
nico dev
20,364 Points

Thank you, Jason!

Great stuff, that was exactly what I was looking for. Indeed, the array_replace particularly seems to do exactly what I was trying to do. It's like you were reading my mind. :)

Thank you again!