Jump to content

Recommended Posts

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.

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.
Edited by requinix

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);
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.