and like us on FB

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

Incidentally, when dealing with a returned array of images, you’ll often want to remove other images (not just the first and last) and you will normally want to reference them by the order in which they’re displayed on a screen. Since an array index starts at 0 and not 1, there’s another relevant function we’ll share shortly that’ll re-index an array to start at 1 (meaning you can remove images by their order on a screen, not the resulting array). When published, you’ll find that function here.

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


Title: Truncate an Array from the "Left" or "Right" with PHP
Description: Truncates an Array from the "Left" or "Right" with PHP.
Download: PHP Code (V0.2) | Plugin Page

Shortt URL for this post: