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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持