如何一次创建多个数组函数?

时间:2018-12-14 03:28:21

标签: php arrays

我有一个数组exp:

Array
(
    [0] => https://openload.co/f/duLIXdarHEM/RARBG.txt
    [1] => https://openload.co/f/C7D4l55xXn8/the.predator.2018.1080p.bluray.x264-sparks.idx
    [2] => https://openload.co/f/9fap6xaoJjQ/the.predator.2018.1080p.bluray.x264-sparks.sub
    [3] => https://openload.co/f/zE7ztog0j9U/the.predator.2018.1080p.bluray.x264-sparks.jpg
    [4] => https://openload.co/f/wjF657ljkVg/the.predator.2018.1080p.bluray.x264-sparks.mkv.mp4
    [5] => https://openload.co/f/4B0qD5ab5bU/Payback.Straight.Up.2006.DC.BRRip.XviD.MP3-XVID.avi
    [6] => https://openload.co/f/4B0qD5ab5bU/Payback.Straight.Up.2006.DC.BRRip.sample.XviD.MP3-XVID.avi
    [7] => https://openload.co/f/b1ihEIUgdDA/2_Eng.srt
)

如何创建一个函数来获取包含“ .mp4”,“。avi”,“。mkv”,...的链接 并删除包含单词“ .txt”,“。jpg”,“。sample”,....的链接。 并将其带回新的连续数组

3 个答案:

答案 0 :(得分:0)

尝试一下:

$a=['https://openload.co/f/duLIXdarHEM/RARBG.txt','https://openload.co/f/b1ihEIUgdDA/2_Eng.srt','https://openload.co/f/4B0qD5ab5bU/Payback.Straight.Up.2006.DC.BRRip.XviD.MP3-XVID.avi'];

$b = array_filter($a, function($v){return preg_match("/(\.mp4){1}|(\.avi){1}|(\.mkv){1}/", $v);});

var_dump($b);

您可以运行检查结果here

答案 1 :(得分:0)

  

使用strpos:

$array = ['https://openload.co/f/duLIXdarHEM/RARBG.txt',
          'https://openload.co/f/C7D4l55xXn8/the.predator.2018.1080p.bluray.x264-sparks.idx', 
          'https://openload.co/f/9fap6xaoJjQ/the.predator.2018.1080p.bluray.x264-sparks.sub', 
          'https://openload.co/f/zE7ztog0j9U/the.predator.2018.1080p.bluray.x264-sparks.jpg', 
          'https://openload.co/f/wjF657ljkVg/the.predator.2018.1080p.bluray.x264-sparks.mkv.mp4'];  

$result = array_filter($array, function($x) {     
    return (strpos($x, '.avi') || strpos($x, '.mp4') || strpos($x, '.mkv'));    
});  
print_r($result);  

希望这会有所帮助!

答案 2 :(得分:0)

因为无论如何您都将需要的链接带回到另一个数组,所以为什么不从原始数组中删除不需要的链接并重新索引它,则可以通过以下方式实现:

`<?php
$array = array( 
"https://openload.co/f/duLIXdarHEM/RARBG.txt",  
"https://openload.co/f/C7D4l55xXn8/the.predator.2018.1080p.bluray.x264-sparks.idx", 
"https://openload.co/f/9fap6xaoJjQ/the.predator.2018.1080p.bluray.x264-sparks.sub", 
"https://openload.co/f/zE7ztog0j9U/the.predator.2018.1080p.bluray.x264-sparks.jpg",      
"https://openload.co/f/wjF657ljkVg/the.predator.2018.1080p.bluray.x264- 
 sparks.mkv.mp4",  
"https://openload.co/f/4B0qD5ab5bU/Payback.Straight.Up.2006.DC.BRRip.XviD.MP3- 
 XVID.avi",
"https://openload.co/f/4B0qD5ab5bU/Payback.Straight.Up.2006.DC.BRRip.sample.XviD.MP3- 
 XVID.avi", 
"https://openload.co/f/b1ihEIUgdDA/2_Eng.srt" );

foreach( $array as $arr ){
if(preg_match('/^.*\.(jpg|txt|sample|)$/i', $arr)){
unset($array[array_search($arr, $array)]);// delete the element
$newArray = array_values($array);// reindex array
}
}

print_r($newArray);`

您可以在操作中here看到它