dongmangsha7354
2012-12-03 20:15
浏览 94
已采纳

在一个数组php中获取多个子字符串匹配

I am attempting to find all rows of an array that contain a specific sub-string.

I have an array containing all my product numbers or SKU's.

I have another array containing paths to images which are found within a directory and its many sub-directories.

(This array of image paths is populated using a RecursiveIterator that goes through the directory and adds all the file paths to this array)

Each image name contains the SKU within it, so sku# 123 may have the following images:
123.jpg
123_1.jpg
123_2.jpg
etc. etc.

I want to output all images that are associated with a particular sku. Here is the code I have started. For some reason I am only getting the desired result for the last sku.

$dir = "./images";          // directory with images
$skus = file("./source.csv");   // source file with all skus
$all_images = array();      // array to hold all image paths found in folder

// recursively search through directory save all file paths to array
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
 $all_images[] = $f;   
}

// loop through each sku and find its image paths
for($i=0; $i < count($skus); $i++){
    $values = preg_grep("/$skus[$i]/", $all_images);
    echo "----sku: $skus[$i] -----<br /><br />";

    foreach($values as $val)
        echo "Values: $val<br />";  
}

My result page looks something like this:

----sku: TR450 -----

----sku: TR451 -----

----sku: TR452 -----

----sku: TR453 -----

----sku: TR454 -----

----sku: TR455 -----

----sku: TR456 -----

Values: ./images\brand\make\TR456 - abacus\TR457.jpg
Values: ./images\brand\make\TR457 - abacus\TR457_Diagram.jpg
Values: ./images\brand\make\TR458 - abacus\Thumbs.db

I'm not sure why this is only working for the last SKU??

Thank you.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douluo6626 2012-12-04 17:13
    已采纳

    I have managed to fix the issue by finding that $skus all had spaces at the end of them. Not sure how the spaces got in there but that caused the pattern not to be recognized!

    ('sku123 ' != 'sku123')
    

    Here is my code:

    $dir = "./images";              // directory with images
    $skus = file("./source.csv");   // source file with all skus
    $skus = array_filter(array_map('trim', $skus)); // this is the only new line, 
                                                    //it removes white space after the sku
                                                    // so the pattern below can actually work.
    $all_images = array();      // array to hold all image paths found in folder
    
    // recursively search through directory save all file paths to array
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
     $all_images[] = $f;   
    }
    
    // loop through each sku and find its image paths
    for($i=0; $i < count($skus); $i++){
        $values = preg_grep("/$skus[$i]/", $all_images);
        echo "----sku: $skus[$i] -----<br /><br />";
    
        foreach($values as $val)
            echo "Values: $val<br />";  
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanmiaosi0150 2012-12-03 20:25

    Try to set your pattern in a variable and pass the variable to preg_grep.

    $pattern = '/'. $skus[$i] . '/';
    $values = preg_grep($pattern, $all_images);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题