2016-11-28 15 views
1

Ich versuche zu finden, welche in jedem Array und wie sie sich unterscheiden, aber es besagt, dass alle nicht im Array mit der Funktion in_array sind.Vergleich zweier Texte php

Ich muss herausfinden, welche entfernt werden sollen und welche hinzugefügt werden müssen, jedes als ein Array zu jquery zurückgeben.

// prepares array 
function prep($string){ 
    $separator = "/n"; 
    $string = explode($separator , $string); 
    $string = array_map('trim', $string); 
    return $string; 
} 

// lines to test 
$lines = 'guest13 /n guest14 /n guest16 /n'; 
$lines2 = 'guest13 /n guest16 /n guest17 /n'; 

// declares storage for later 
$add = array(); 
$rem = array(); 

// tests lines to see if the same 
similar_text($lines, $lines2, $percent); 

// declares arrays 
$linestest = array(); 
$linestest2 = array(); 

// filters lines to be tested 
$linestest = prep($lines); 
$lines2test = prep($lines2); 

if ($percent != 100) { 

    // checks if it needs to be removed 
    foreach ($linestest as $line => $mow) { 

     if (!in_array('$mow', $linestest2, true)) { 
      $rem[] = $mow; 
      echo '<br />remove ' . $mow; 
     } 
    } 
    foreach ($lines2test as $sigh => $wow) { 

     if (!in_array('$wow', $linestest, true)) { 
      $add[] = $wow; 
      echo '<br />add ' . $wow; 
     } 
     // echos each out 
    } 
    foreach ($rem as $new) { 
     $mows .= 'remove this ' . $new; 
    } 
    foreach ($add as $new) { 
     $mows .= 'add this ' . $new; 
    } 
    echo '<br />end of string ' . $mows; 
} 

Antwort

1

Es scheint, dass Sie mehrere Fehler haben. Ich habe Ihre Kommentare entnommen und meine eigenen, damit Sie besser verstehen, was ich geändert:

function prep($string) { 
    $separator = "/n"; 
    // added the following line to prevent empty value in array 
    $string = trim($string, $separator); 
    $string = explode($separator, $string); 
    $string = array_map('trim', $string); 
    return $string; 
} 

$lines = 'guest13 /n guest14 /n guest16 /n'; 
$lines2 = 'guest13 /n guest16 /n guest17 /n'; 

$add = array(); 
$rem = array(); 

similar_text($lines, $lines2, $percent); 

$linestest = array(); 
$linestest2 = array(); 

$linestest = prep($lines); 
$lines2test = prep($lines2); 

if ($percent != 100) { 
    $mows = ''; // added this line to initialize variable and prevent undefined notice 
    foreach ($linestest as $mow) { // removed key declaration, you're not using it 
     // changed the following from !in_array to in_array 
     // using $lines2test instead of $linestest2 
     if (in_array($mow, $lines2test, true)) { 
      $rem[] = $mow; 
      echo '<br />remove ' . $mow ; 
     } 
    } 
    foreach ($lines2test as $wow) { // removed key declaration, you're not using it 
     // using $wow instead of $cow 
     if (!in_array($wow, $linestest, true)) { 
      $add[] = $wow; 
      echo '<br />add ' . $wow; 
     } 
    } 
    foreach ($rem as $new) { 
     $mows .= 'remove this ' . $new; 
    } 
    foreach ($add as $new) { 
     $mows .= 'add this ' . $new; 
    } 
    echo '<br />end of string ' . $mows; 
} 

Ausgang:

remove guest13 
remove guest16 
add guest17 
end of string remove this guest13remove this guest16add this guest17 

Wenn Sie einen kürzeren Weg wollen es ohne die Schleifen zu tun, hier ist eine verkürzte Version:

function prep($string, $separator = '/n') { 
    return array_map('trim', explode($separator, trim($string, $separator))); 
} 

$lines = 'guest13 /n guest14 /n guest16 /n'; 
$lines2 = 'guest13 /n guest16 /n guest17 /n'; 

$arr1 = prep($lines); 
$arr2 = prep($lines2); 

// all values from $arr1 present in $arr2 
// outputs: guest13 and guest16 
$remove = array_intersect($arr1, $arr2); 

// all values from $arr2 NOT present in $arr1 
// outputs: guest17 
$add = array_diff($arr2, $arr1); 

// alternative to above, if you want both guest14 and guest17 
$add = array_diff($arr1, $arr2) + array_diff($arr2, $arr1); 
0

Ich habe es ziemlich viel in eine Funktion stecken und einige Teile zwicken, um es klarer zu machen. Alles, was Sie tun müssen, ist das übergebene String-Format als die beiden Parameter. Die Rückgabe ist das Array der Gäste, die im 2D-Array HINZUFÜGEN und ENTFERNEN. Ich habe die if($percent != 100) nicht hinzugefügt, aber ich bin sicher, dass Sie diese Variable als Parameter übergeben und diese Zeile innerhalb der Funktion einfügen können.

Der Code ist stark kommentiert, also wird es hoffentlich klar sein.

// lines to test 
$lines = 'guest13 /n guest14 /n guest16 /n'; 
$lines2 = 'guest13 /n guest16 /n guest17 /n'; 

$addArray = checkData($lines, $lines2); //Call check() function 

var_dump($addArray); //Check return of array is correct 

function checkData($inputOne, $inputTwo) { //Two strings as paramters in the format given 

    $arrayOne = explode('/n', str_replace(' ', '', $inputOne)); //Replace whitespace with blank 
    $arrayTwo = explode('/n', str_replace(' ', '', $inputTwo)); //Replace whitespace with blank 

    foreach($arrayOne as $value) { //First Compare ArrayOne 

     if(in_array($value, $arrayTwo)) { //Check to see if iteration of loop value is in ArrayTwo 

      $rem[] = $value; 
      echo '<br />remove ' . $value; 

     } elseif (!in_array($value, $arrayTwo)) { //Add to array if not in Array Two 

      $add[] = $value; 
      echo '<br /> add ' . $value; 
     }  
    } 

    foreach($arrayTwo as $value) { 

     if(!in_array($value, $arrayOne)) { //Check to see if iteration of loop value is NOT in ArrayOne 

      $add[] = $value; 
      echo '<br /> add ' . $value; 

     } elseif (!in_array($value, $rem)) { //Compare against Array Rem to make sure we dont add duplicates 

      $rem[] = $value; 
      echo '<br />remove ' . $value; 
     } 
    } 

    $outArray = ['Add' => $add, 'Remove' => $rem]; //2D Array 

    return $outArray; //Return Array 
} 

Ausgabe

remove guest13 
add guest14 
remove guest16 
add guest17 

array(2) { ["Add"]=> array(2) { [0]=> string(7) "guest14" [1]=> string(7) "guest17" } ["Remove"]=> array(3) { [0]=> string(7) "guest13" [1]=> string(7) "guest16"} } 

bearbeiten!

Misread Frage und bearbeitet, so dass es nun beide Arrays in 2D-Array zurückgibt.