donmqryh49993 2013-12-18 21:07
浏览 37
已采纳

如何将数组拆分为2个不同的数组

Ive got an array with URLs in it like so:

$urls = array("http://myurl.com/file/2222/file.rar", "http://myurl.com/file/2222/file.part1.rar", "http://myurl.com/file/2222/file.part2.rar", "http://myurl/file/2222/file.part3.rar", "http://myurl.com/file/2222/file.part4.rar");

I want to find any of the filenames to check if they are a part or a single file.

For example the result for the above should be:

$single = "http://myurl.com/file/2222/file.rar";
$splits = array("http://myurl.com/file/2222/file.part1.rar", "http://myurl.com/file/2222/file.part2.rar", "http://myurl.com/file/2222/file.part3.rar", "http://myurl.com/file/2222/file.part4.rar");

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dtdvbf37193 2013-12-18 21:17
    关注

    I like preg_grep():

    $parts = preg_grep('/\.part\d+\.rar/', $urls);
    $singles = array_diff($urls, $parts);
    

    Or to do the opposite for the singles:

    $singles = preg_grep('/\.part\d+\.rar/', $urls, PREG_GREP_INVERT);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?