Truncate an Array from the “Left” or “Right” with PHP

In our article on importing Imgur images into WordPress, we used a function that would truncate the returned data either side of the array to remove an introductory image or the trailing image (Imgur people have a bad habit of throwing in a “cat tax”). Those images aren’t relevant if you want to use the data yourself, so the function we employed removed them (the attribute of remove="1,2" would remove the first image and the last two).

Incidentally, we also used the function from the last post on indexing an array in our Imgur shortcode so images could be removed by reference to the order in which they rendered on a screen.

If you used our Imgur shortcode you may be familiar with the function described below.

The Result

Consider the following array:

We can remove the first element and the last two by using the following:

The result:

The PHP Function

Download Example Code

Download the code with the above example here .

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *