这是我的数组$ youtubeabout'转储;
array (size=5)
0 =>
array (size=310)
0 => string 'https://www.youtube.com/channel/UCFFE6wa2VsYQAfhkMZCnLPQ' (length=56)
1 => string 'https://www.youtube.com/channel/UCFFE6wa2VsYQAfhkMZCnLPQ' (length=56)
2 => string 'https://www.youtube.com/channel/UCYBzff129L5OrVs6UUICW0g' (length=56)
3 => string 'https://www.youtube.com/channel/UCYBzff129L5OrVs6UUICW0g' (length=56)
4 => string 'https://www.youtube.com/channel/UCz5jvb6SUgwDftN7zHfUDoQ' (length=56)
5 => string 'https://www.youtube.com/channel/UCz5jvb6SUgwDftN7zHfUDoQ' (length=56)
我的阵列很少; 正如您可以看到一些重复出现的值。 如果他们像那样回忆,我试图得到它们;
$getthesearchpage = file_get_contents('goog.txt');
preg_match_all('/((http|https):\/\/|)(www.|)youtube\.com\/(channel\/|user\/)[a-zA-Z0-9\-_]{1,}/', $getthesearchpage, $youtubeabout);
$i = 0;
while ($youtubeabout[0][$i] != $youtubeabout[0][$i++]){
print_r($youtubeabout[0]);
$i++;
}
`
怎么办?
答案 0 :(得分:1)
不知道我是否理解你的问题,但如果你想要数组中的唯一元素,那么就有一个PHP函数:array_unique()
$getthesearchpage = file_get_contents('goog.txt');
preg_match_all('/((http|https):\/\/|)(www.|)youtube\.com\/(channel\/|user\/)[a-zA-Z0-9\-_]{1,}/', $getthesearchpage, $youtubeabout);
$youtubeabout[0] = array_unique($youtubeabout[0]);
var_dump($youtubeabout[0]); // print that
输出:
array(3) {
[0]=>
string(56) "https://www.youtube.com/channel/UCFFE6wa2VsYQAfhkMZCnLPQ"
[2]=>
string(56) "https://www.youtube.com/channel/UCYBzff129L5OrVs6UUICW0g"
[4]=>
string(56) "https://www.youtube.com/channel/UCz5jvb6SUgwDftN7zHfUDoQ"
}
您可以迭代该数组并删除多于或少于57个字符的数据:
foreach ($youtubeabout[0] as $k => $v) {
if (strlen($v)!=57)
unset($youtubeabout[0][$k]);
}