【问题标题】:How to split array into elements based [closed]如何将数组拆分为基于元素的[关闭]
【发布时间】:2013-09-19 08:11:30
【问题描述】:

我正在使用scandir() 在包含诸如R12345.jpg, R12345_2.jpg, R12346.jpg, R12346_2.jpg, R12346_3.jpg 等文件的目录中构建图像文件名数组。

我想知道将这个数组拆分为以下元素的最佳方法:

R12345,,jpg
R12345,2,jpg
R12346,,jpg
R12346,2,jpg
R12346,3,jpg

谢谢。

【问题讨论】:

  • 数组中的值是否存在模式?如果没有,那么您可能无法拆分它。
  • 使用正则表达式匹配文件名的各个部分。

标签: php arrays split


【解决方案1】:

你可以试试:

$input  = 'R12345_2.jpg';  
$output = explode(',', str_replace(array('.', '_'), ',', $input);

它会返回一个数组:

array (size=3)
  0 => string 'R12345' (length=6)
  1 => string '2' (length=1)
  2 => string 'jpg' (length=3)

或者使用正则表达式:

preg_match('/^([^_]*)_?(\d+)?\.(.*)$/', $input, $output);

输出:

array (size=4)
  0 => string 'R12345.jpg' (length=10)
  1 => string 'R12345' (length=6)
  2 => string '' (length=0)
  3 => string 'jpg' (length=3)

array_shift($output);

您可以快速删除第一个元素。

【讨论】:

  • 这不会为R12345.jpg 做正确的事情。
  • @Barmar 你说得对,已删除。
  • 很遗憾文件名的R12345部分长度不一致......
【解决方案2】:

$files_arr  //this contains your files
$final_arr=array();  //this contains final array

foreach($files_arr as $k=>$v)
{
    $test_arr = explode('.',$v);
    $t = explode('_',$test_arr[0]);
    if(empty($t[1]))    $t[1] = '';
    $t[2] = $test_arr[1];

    array_push($final_arr[$k],$t);
}
print_r($final_arr);

【讨论】:

  • 这与 haz 的第一个答案相同,请参阅我的评论。
  • 抱歉,请检查我的编辑。
【解决方案3】:

使用explode()函数分割初始数组,然后使用preg_match分割单个部分:

$str = 'R12345.jpg, R12345_2.jpg, R12346.jpg, R12346_2.jpg, R12346_3.jpg';
$files = explode(',', $str);

$result = array();
foreach ($files as $file) {
    $matches = array();

    preg_match('/^([A-Za-z0-9]+)_?([0-9]*)\.(jpg|png|gif)/$', trim($file), $matches);

    array_shift($matches); // Remove first match that is the whole file name

    array_push($result, $matches);
}

【讨论】:

    【解决方案4】:
    <?php
    //Result of $files = implode(', ',$scandirResult);
    $files = 'R12345.jpg, R12345_2.jpg, R12346.jpg, R12346_2.jpg, R12346_3.jpg';
    preg_match_all('#((R\d+)_?(\d*))\.(\w+)#',$files,$output);
    $result = array();
    foreach($output[2] as $key => $name)
    {
        $result[] = array($name,$output[3][$key],$output[4][$key]);
    }
    echo '<pre>'.print_r($result,true).'</pre>';
    ?>
    

    输出:

    Array
    (
        [0] => Array
            (
                [0] => R12345
                [1] => 
                [2] => jpg
            )
    
        [1] => Array
            (
                [0] => R12345
                [1] => 2
                [2] => jpg
            )
    
        [2] => Array
            (
                [0] => R12346
                [1] => 
                [2] => jpg
            )
    
        [3] => Array
            (
                [0] => R12346
                [1] => 2
                [2] => jpg
            )
    
        [4] => Array
            (
                [0] => R12346
                [1] => 3
                [2] => jpg
            )
    
    )
    

    【讨论】:

    • 这正是我所追求的。谢谢@PeterSmith
    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 2021-08-04
    • 1970-01-01
    • 2020-09-06
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多