为什么我的array_diff不起作用?

时间:2013-02-11 23:48:18

标签: php array-difference

我尝试了以前回答的问题的多个代码片段,这些代码片段都以不同的方式执行相同的操作,即从数组中删除常用字,具有不同的内置和自定义PHP函数但我仍然无法获得此片段工作的代码使我认为还有其他根本性的错误。

$tweets = 0;
$tweetarray = array();
$tweettext = fopen('tweets.txt', 'w+');

foreach ($results['results'] as $value) { //write text of tweets to text file
$tweets++;
$tweetarray[$value['from_user']] = $value['text'];

$text = $value['text'];
fwrite($tweettext, $text.'\n');
}

$text = file_get_contents('tweets.txt',NULL); 
$text=str_replace("\r\n"," ",$text); 
$text=preg_replace('/[^a-zA-Z\s]/','',$text); 
$text=preg_replace('/(\s){2,}/',' ',$text); 
$text=strtolower($text); 
$text=explode(" ",$text);

$veryrawsearch = explode(" ", $_POST['search']);
$searchwords = array_map('strtolower', $veryrawsearch);
$common = file("commonwords.txt");
$wordlist = array_merge($searchwords, $common);

$safetext = array_diff($text, $wordlist);

$out=array_count_values($safetext);
arsort($out); 
$out=array_slice($out,0,3); 

fclose($tweettext);

输出结果为:

while ( list($key, $value) = each($out) ) {
    echo ('<a href="#">'.$key.'</a> ~ ');
}

代码不会产生任何错误,如果我更改此行:

$safetext = array_diff_assoc($text, $wordlist);

$safetext = array_diff_assoc($text, $searchwords);

代码正常运行。

当我通过将行更改为

来测试第二个数组时
$safetext = array_diff_assoc($text, $common);

代码失败且没有错误,它只是不删除$ text数组中的常用字。

我使用print_r()输出了两个数组;它们看起来格式正确,例如

$searchwords:
Array ( [0] => gordon [1] => ramsey ) //sample search term
$common:
Array ( [0] => a [1] => ii [2] => about [3] => above [4] => according //...sample content
$wordlist:
Array ( [0] => gordon [1] => ramsey [2] => a [3] => ii [4] => about [5] => above [6] => according //merged arrays

所有三个数组看起来都是正确形成的,两个原始数据和合并数据,但只有$ searchwords在测试时才会被过滤。

0 个答案:

没有答案