2016-11-07 6 views
-1

Ich habe 2 Array in meinem Code, wie die unten gezeigt:array_diff funktioniert nicht (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); 
 
?>

$ Stoppwort einiger Stoppwort enthalten wie in http://xpo6.com/list-of-english-stop-words/ angebracht

Alles, was ich tun möchte, ist: Ich möchte überprüfen, ob das Element in Array $ Kata speichern und es ist nicht in Array $ stopword

Also ich möchte alle Elemente löschen, die in beiden Array $ Kata und $ Stoppwort vorhanden sind. Ich lese einen Vorschlag, array_diff zu verwenden, aber irgendwie funktioniert es mir nicht. Wirklich brauchen Ihre Hilfe :(Dank

+1

Sie hilft Ihnen erwarten, dass wir Inhalte von '$ kata' zu erraten,' $ stopword'? –

+0

Ich bearbeite es bereits. Es tut uns leid. – Berlian

Antwort

0

array_diff ist, was Sie brauchen, du hast recht Hier ist eine vereinfachte Version von dem, was Sie versuchen zu tun:..

<?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', '...'] 

Sie sollten auch den Wert von $stopword doppeltzukontrollieren , kann ich diesen Teil nicht testen (nicht Ihre Datei hat). Wenn es nicht für Sie arbeitet, ich denke, das Problem mit dieser Variable ($stopword)

0

es gibt ein Problem in Ihrem $stopword Array. var_dump es, um das Problem zu sehen. array_diff funktioniert korrekt.

Versuchen Sie folgenden Code schrieb ich Ihre $stopword Array richtig zu machen:

<?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>"; 
    ?> 

Ich hoffe, dass es