2016-11-07 101 views
-1

我有2陣列在我的代碼,就像下面的圖所示:和array_diff不起作用(PHP)

<?php 
 

 
$kalimat = "I just want to search something like visual odometry, dude"; 
 
$kata = array(); 
 
$eliminasi = " \n . ,;:-()?!"; 
 
$tokenizing = strtok($kalimat, $eliminasi); 
 

 
while ($tokenizing !== false) { 
 
\t $kata[] = $tokenizing; 
 
\t $tokenizing = strtok($eliminasi); 
 
} 
 
$sumkata = count($kata); 
 
print "<pre>"; 
 
print_r($kata); 
 
print "</pre>"; 
 

 

 
//stop list 
 
$file = fopen("stoplist.txt","r") or die("fail to open file"); 
 
$stoplist; 
 
$i = 0; 
 
while($row = fgets($file)){ 
 
\t $data = explode(",", $row); 
 
\t $stoplist[$i] = $data; 
 
\t $i++; 
 
} 
 
fclose($file); 
 
$count = count($stoplist); 
 

 
//Cange 2 dimention array become 1 dimention 
 
for($i=0;$i<$count;$i++){ 
 
for($j=0; $j<1; $j++){ 
 
\t $stopword[$i] = $stoplist[$i][$j]; 
 
} 
 
} \t 
 

 
//Filtering process 
 
$hasilfilter = array_diff($kata,$stopword); 
 
var_dump($hasilfilter); 
 
?>

$禁用詞包含一些停用詞像附着在http://xpo6.com/list-of-english-stop-words/

我想要做的是:我想檢查是否保存數組$ kata中存在的元素,並且它不存在於數組$ stopword

所以我想刪除數組$ kata和$ stopword中存在的所有元素。 我讀了一些建議使用array_diff,但不知何故,它不適用於我。真的需要你的幫助:(謝謝

+1

你希望我們猜測'$ kata','$ stopword'的內容是什麼? –

+0

我已經編輯它。抱歉。 – Berlian

回答

0

array_diff是你需要什麼,你就在這裏爲你努力做一個簡化版本:。

<?php 

// Your string $kalimat as an array of words, this already works in your example. 
$kata = ['I', 'just', 'want', 'to', '...']; 

// I can't test $stopword code, because I don't have your file. 
// So let's say it's a array with the word 'just' 
$stopword = ['just']; 

// array_diff gives you what you want 
var_dump(array_diff($kata,$stopword)); 

// It will display your array minus "just": ['I', 'want', 'to', '...'] 

你也應該仔細檢查的$stopword值,我無法測試這個部分(沒有你的文件)如果它不適合你,我想這個問題是由這個變量($stopword

0

你的$stopword數組有問題var_dump它看到的問題。array_diff正在工作正確。

試試下面的代碼我寫的,讓您的$stopword陣列權:

<?php 

    $kalimat = "I just want to search something like visual odometry, dude"; 
    $kata = array(); 
    $eliminasi = " \n . ,;:-()?!"; 
    $tokenizing = strtok($kalimat, $eliminasi); 

    while ($tokenizing !== false) { 
     $kata[] = $tokenizing; 
     $tokenizing = strtok($eliminasi); 
    } 
    $sumkata = count($kata); 
    print "<pre>"; 
    print_r($kata); 
    print "</pre>"; 

    //stop list 
    $file = fopen("stoplist.txt","r") or die("fail to open file"); 
    $stoplist; 
    $i = 0; 
    while($row = fgets($file)){ 
     $data = explode(",", $row); 
     $stoplist[$i] = $data; 
     $i++; 
    } 
    fclose($file); 
    $count = count($stoplist); 
    //Cange 2 dimention array become 1 dimention 
    $stopword= call_user_func_array('array_merge', $stoplist); 
    $new = array(); 
    foreach($stopword as $st){ 
     $new[] = explode(' ', $st); 
    } 
    $new2= call_user_func_array('array_merge', $new); 
    foreach($new2 as &$n){ 
     $n = trim($n); 
    } 
    $new3 = array_unique($new2); 
    unset($stopword,$new,$new2); 
    $stopword = $new3; 
    unset($new3); 

    //Filtering process 
    $hasilfilter = array_diff($kata,$stopword); 
    print "<pre>"; 
    var_dump($hasilfilter); 
    print "</pre>"; 
    ?> 

我希望它能幫助