duanqin7791 2018-09-27 18:34
浏览 19
已采纳

PHP组将某些结果从foreach on array转换为另一个数组

I have an array that looks something like this:

$array =  array( [0] => FILE-F01-E1-S01.pdf 
                 [1] => FILE-F01-E1-S02.pdf 
                 [2] => FILE-F01-E1-S03.pdf 
                 [3] => FILE-F01-E1-S04.pdf 
                 [4] => FILE-F01-E1-S05.pdf 
                 [5] => FILE-F02-E1-S01.pdf 
                 [6] => FILE-F02-E1-S02.pdf 
                 [7] => FILE-F02-E1-S03.pdf );

Basically, I need to look at the first file and then get all the other files that have the same beginning ('FILE-F01-E1', for example) and put them into an array. I don't need to do anything with the other ones at this point.

I've been trying to use a foreach loop finding the previous value to do this, but am not having any luck.

Like this:

$previousFile = null;

foreach($array as $file)
{

    if(substr_replace($previousFile, "", -8) == substr_replace($file, "", -8)) 
    {
        $secondArray[] = $file;
     }

    $previousFile = $file;
}

So then $secondArray would look like this:

    Array ( [0] => FILE-F01-E1-S01.pdf [1] => FILE-F01-E1-S02.pdf 
            [2] => FILE-F01-E1-S03.pdf [3] => FILE-F01-E1-S04.pdf 
            [4] => FILE-F01-E1-S05.pdf)

As my result.

Thank you!

  • 写回答

4条回答 默认 最新

  • dongmei2351 2018-09-27 18:49
    关注

    Are you sure this will be the naming format? That is crucial information to have to construct a regexp or something to check for being a substring of the following strings.

    If we can assume this and that the "base" name is always at index 0 then you could do something like.

    <?php
        $myArr =  [ 
                    'FILE-F01-E1-S01.pdf', 
                    'FILE-F01-E1-S02.pdf', 
                    'FILE-F01-E1-S03.pdf', 
                    'FILE-F01-E1-S04.pdf', 
                    'FILE-F01-E1-S05.pdf', 
                    'FILE-F02-E1-S01.pdf', 
                    'FILE-F02-E1-S02.pdf', 
                    'FILE-F02-E1-S03.pdf'
                  ];
    
        $baseName        = '';
        $allSimilarNames = [];
    
        foreach($myArr as $index => &$name) {
          if($index == 0) {
              $baseName = substr($name, 0, strrpos($name, '-'));
              $allSimilarNames[] = $name;
          }
          else {
              if(strpos($name, $baseName) === 0) {
                  $allSimilarNames[] = $name;
              }
          }
        }
    
        var_dump($allSimilarNames);
    

    This will

    • Check at index one to get the base name to compare against
    • Loop all items in the array and match all items, no matter where in the array they are, that are similar according to your naming convention

    So if you next time have an array that is

    $myArr =  [ 
                        'FILE-F02-E1-S01.pdf',
                        'FILE-F01-E1-S01.pdf', 
                        'FILE-F01-E1-S02.pdf', 
                        'FILE-F01-E1-S03.pdf', 
                        'FILE-F01-E1-S04.pdf', 
                        'FILE-F01-E1-S05.pdf', 
                        'FILE-F02-E1-S02.pdf', 
                        'FILE-F02-E1-S03.pdf'
                      ]; 
    

    this will return all the items that match FILE-F02-E1*.

    You could also make a small function of it for easier use and not have to rely on the element at index 0 having to be the "base" name.

    <?php
    function findMatches($baseName, &$names) {
        $matches = [];
        $baseName = substr($baseName, 0, strrpos($baseName, '-'));
        foreach($names as &$name) {
            if(strpos($name, $baseName) === 0) {
              $matches[] = $name;
            }
        }
    
        return $matches;
    }
    
    $myArr =  [ 
                'FILE-F01-E1-S01.pdf', 
                'FILE-F01-E1-S02.pdf', 
                'FILE-F01-E1-S03.pdf', 
                'FILE-F01-E1-S04.pdf', 
                'FILE-F01-E1-S05.pdf', 
                'FILE-F02-E1-S01.pdf', 
                'FILE-F02-E1-S02.pdf', 
                'FILE-F02-E1-S03.pdf'
              ];
    
    $allSimilarNames = findMatches('FILE-F01-E1-S01.pdf', $myArr);
    
    var_dump($allSimilarNames);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

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