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条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题