[英]How to split up an array into 2 different arrays
Ive got an array with URLs in it like so: 我有一个包含URL的数组,如下所示:
$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! 提前致谢!
I like preg_grep()
: 我喜欢
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.