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

在一个数组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 />";  
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊