anderson_catchme Posted September 3, 2014 Share Posted September 3, 2014 So I'm trying to unset array elements that don't contain "thumbnail" in the filename. Seems simple, but it's not working. function generateImage3 ($category, $imageornot, $Imagepointer) { if($imageornot == 1){ $var = get_cwd_uploads().'\\'.$Imagepointer; // get_cwd_uploads() is a custom function $scanned_directory = array_diff(scandir($var), array('..', '.')); foreach($scanned_directory as $elements){ $pos = strpos($elements, 'thumbnail'); if($pos === FALSE){ unset($scanned_directory[key($elements)]); } } // irrelevant code Any help appreciated. Link to comment https://forums.phpfreaks.com/topic/290818-eliminating-array-elements-not-containing-string/ Share on other sites More sharing options...
anderson_catchme Posted September 3, 2014 Author Share Posted September 3, 2014 Nevermind, solved it. Mods you may delete this. Link to comment https://forums.phpfreaks.com/topic/290818-eliminating-array-elements-not-containing-string/#findComment-1489721 Share on other sites More sharing options...
requinix Posted September 3, 2014 Share Posted September 3, 2014 We don't really like deleting stuff. What we would like is if you shared what you had to do to fix your problem. And while I'm here, next time please actually describe the problem ("it's not working" doesn't mean much) and use tags around your code. Link to comment https://forums.phpfreaks.com/topic/290818-eliminating-array-elements-not-containing-string/#findComment-1489722 Share on other sites More sharing options...
AbraCadaver Posted September 3, 2014 Share Posted September 3, 2014 You want to use the key from the foreach. Also you want to use !== false because it exists: foreach($scanned_directory as $key => $elements){ $pos = strpos($elements, 'thumbnail'); if($pos !== FALSE){ unset($scanned_directory[$key]); } } However this is much simpler (replaces all of your code): $files = preg_grep("/thumbnail/", glob("$var/*.*"), PREG_GREP_INVERT); Link to comment https://forums.phpfreaks.com/topic/290818-eliminating-array-elements-not-containing-string/#findComment-1489829 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.