www.webdeveloper.com
Results 1 to 2 of 2

Thread: ARRAY_DIFF partial (wildcard) match

  1. #1
    Join Date
    Feb 2003
    Posts
    102

    ARRAY_DIFF partial (wildcard) match

    Hi all,

    I am reading a list of files from a directory into an array named $filenamearray. I can filter my array with full string matches as follows:
    Code:
    $filenamearrayfilter = array("ADmap01.pdf","ADmap02.pdf","ADmap03.pdf","SYmap01.pdf","SYmap02.pdf");
    $filenamearray = array_diff($filenamearray,$filenamearrayfilter);
    As you can see, the two-letter prefix of my files, and the document number will always be different, but they will all have a common PART-MATCH string, ie: "map".

    So what I would like to be able to do is:
    Code:
    $filenamearrayfilter = array("*map*");
    $filenamearray = array_diff($filenamearray,$filenamearrayfilter);
    ...that is, I want to be able to have a partial match using array_diff.

    I have tried many combinations of array_diff, in_array, array_search, but thus far have not been able to get anything to work for me.


    Does anyone have any suggestions?


    Cheers

    Chris

  2. #2
    Join Date
    Feb 2003
    Posts
    102
    I came up with a solution using two separate loops as follows...

    Code:
    //setup a second empty array to contain the filtered elements from the first array
    $FILTEREDfilenamearray = array();
    
    //read through the first array of filenames (aka:$filenamearray) and for any individual elements that match "map", push that element onto the end of our new $FILTEREDfilenamearray
    foreach($filenamearray as $element){
        if (strrpos($element, "map")) {
    	    array_push($FILTEREDfilenamearray, $element);
        }
    }
    
    //now we can loop through the second array containing only the filtered results.
    foreach ($FILTEREDfilenamearray as $filename) {
        echo $filename."<br>";
    }
    Still, if there is a built-in function to handle this, please let me know as I am still a PHP noob!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles