PHP - Remove items from an array with given parameter

Protocol Zero

I've searched around and I found some similar questions asked, but none that really help me (as my PHP abilities aren't quite enough to figure it out). I'm thinking that my question will be simple enough to answer, as the similar questions I found were solved with one or two lines of code. So, here goes!

I have a bit of code that searches the contents of a given directory, and provides the files in an array. This specific directory only has .JPG image files named like this:

Shot01.jpg Shot01_tn.jpg

so on and so forth. My array gives me the file names in a way where I can use the results directly in an tag to be displayed on a site I'm building. However, I'm having a little trouble as I want to limit my array to not return items if they contain "_tn", so I can use the thumbnail that links to the full size image. I had thought about just not having thumbnails and resizing the images to make the PHP easier for me to do, but that feels like giving up to me. So, does anyone know how I can do this? Here's the code that I have currently:

$path = 'featured/';
$newest = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS));
$array = iterator_to_array($newest);
foreach($array as $fileObject):
$filelist = str_replace("_tn", "", $fileObject->getPathname());
echo $filelist . "<br>";
endforeach;

I attempted to use a str_replace(), but I now realize that I was completely wrong. This returns my array like this:

Array
(
[0] => featured/Shot01.jpg
[1] => featured/Shot01.jpg
[2] => featured/Shot02.jpg
[3] => featured/Shot02.jpg
[4] => featured/Shot03.jpg
[5] => featured/Shot03.jpg
)

I only have 3 images (with thumbnails) currently, but I will have more, so I'm also going to want to limit the results from the array to be a random 3 results. But, if that's too much to ask, I can figure that part out on my own I believe.

So there's no confusion, I want to completely remove the items from the array if they contain "_tn", so my array would look something like this:

Array
(
[0] => featured/Shot01.jpg
[2] => featured/Shot02.jpg
[4] => featured/Shot03.jpg
)

Thanks to anyone who can help!

redy
<?php

function filtertn($var)
{
   return(!strpos($var,'_tn'));
}


$array = Array(
    [0] => featured/Shot01.jpg
    [1] => featured/Shot01_tn.jpg
    [2] => featured/Shot02.jpg
    [3] => featured/Shot02_tn.jpg
    [4] => featured/Shot03.jpg
    [5] => featured/Shot03_tn.jpg
    );

$filesarray=array_filter($array, "filtertn");

print_r($filesarray);
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove items from multidimensional array in PHP

From Dev

Remove items from array

From Dev

php - remove an element from array of objects by given key

From Dev

Remove items from S3 objects array in php

From Dev

How to remove items with certain length from array in php

From Dev

Is it possible to remove items from a array?

From Dev

KnockoutJS: Remove array of items from an array

From Dev

Remove items from one array if not in the second array

From Dev

KnockoutJS: Remove array of items from an array

From Dev

Remove element from array if given in argument

From Dev

remove specific id from array of checked items

From Dev

How to remove multiple items from a swift array?

From Dev

Remove items from based of substring in array jquery

From Dev

Remove set of items from an array jQuery

From Dev

Remove items from array to ensure no duplicate strings

From Dev

Remove items from array without looping

From Dev

AngularJS remove items from array compared by id

From Dev

How to remove multiple items from a swift array?

From Dev

How to remove items from an array of objects?

From Dev

AngularFire how to remove multiple items from array?

From Dev

Using a callback to remove duplicate items from an array

From Dev

Returning specific items from an array given an array of coordinates in MATLAB

From Dev

Create the Array from the given array in PHP

From Dev

getting sub array from the given array in php

From Dev

PHP Array Create from given Array

From Dev

Remove duplicate items from S3 objects multi dimension array in php

From Dev

Remove duplicate items from S3 objects multi dimension array in php

From Dev

array_push expects one parameter to be array null given in PHP?

From Dev

Remove n items after every n items from an array

Related Related

  1. 1

    Remove items from multidimensional array in PHP

  2. 2

    Remove items from array

  3. 3

    php - remove an element from array of objects by given key

  4. 4

    Remove items from S3 objects array in php

  5. 5

    How to remove items with certain length from array in php

  6. 6

    Is it possible to remove items from a array?

  7. 7

    KnockoutJS: Remove array of items from an array

  8. 8

    Remove items from one array if not in the second array

  9. 9

    KnockoutJS: Remove array of items from an array

  10. 10

    Remove element from array if given in argument

  11. 11

    remove specific id from array of checked items

  12. 12

    How to remove multiple items from a swift array?

  13. 13

    Remove items from based of substring in array jquery

  14. 14

    Remove set of items from an array jQuery

  15. 15

    Remove items from array to ensure no duplicate strings

  16. 16

    Remove items from array without looping

  17. 17

    AngularJS remove items from array compared by id

  18. 18

    How to remove multiple items from a swift array?

  19. 19

    How to remove items from an array of objects?

  20. 20

    AngularFire how to remove multiple items from array?

  21. 21

    Using a callback to remove duplicate items from an array

  22. 22

    Returning specific items from an array given an array of coordinates in MATLAB

  23. 23

    Create the Array from the given array in PHP

  24. 24

    getting sub array from the given array in php

  25. 25

    PHP Array Create from given Array

  26. 26

    Remove duplicate items from S3 objects multi dimension array in php

  27. 27

    Remove duplicate items from S3 objects multi dimension array in php

  28. 28

    array_push expects one parameter to be array null given in PHP?

  29. 29

    Remove n items after every n items from an array

HotTag

Archive