2

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!

2
  • Take a look at PHP's array_filter() function. It is designed exactly for what you are looking for: php.net/manual/de/function.array-filter.php Commented Sep 19, 2013 at 6:22
  • @Prix Very bad idea. It's better to just not add duplicates. Commented Sep 19, 2013 at 6:22

5 Answers 5

1
<?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);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Worked perfectly! I know everyone kinda said use the strpos(), but your specific method is my preferred one. Least amount of actual coding, that gives me exactly what I want! I can't vote up, otherwise I would! Thanks again!
1

Just use stripos() function to check if filename contains _tn string. If not, add to array.

Comments

0
Use this
<?php
$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
          )

foreach($array as $k=>$filename):
   if(strpos($filename,"_tn")){
      unset($array[$k]);
    }
endforeach;

Prnt_r($array);

//OutPut will be you new array removed all name related _tn files

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

?>

Comments

0

I can't understand what is the problem? Is it required to add "_tn" to array? Just check "_tn" existence and don't add this element to result array.

Comments

0

Try strpos() to know if filename contains string "_tn" or not.. if not then add filename to array

$path = 'featured/';
$newest = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path,    RecursiveDirectoryIterator::SKIP_DOTS));
$array = iterator_to_array($newest);
$filesarray = array();
foreach($array as $fileObject):
     // Check - string contains "_tn" substring or not
     if(!strpos($fileObject->getPathname(), "_tn")){
       // Check - value already exists in array or not
       if(!in_array($fileObject->getPathname(), $filesarray)){ 
          $filesarray[] = $fileObject->getPathname();
       }
    }
endforeach;
print_r($filesarray); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.