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

在一个数组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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里