2016-11-28 143 views
1

我想找到哪些在每個數組中,以及它們如何不同,但它說所有不在in_array函數的數組中。比較兩個文本php

我需要找到哪些要刪除,哪些需要添加,作爲數組返回每個jquery。

// 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; 
} 

回答

1

看來你有幾個錯誤。我已經刪除您的意見和加入我自己的,所以你可以更好地瞭解我改變:

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; 
} 

輸出:

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

如果你想有一個較短的方式做到這一點不循環,這裏是一個濃縮版:

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

我已經差不多堅持了各成一體功能以及調整一些部分,使其更清晰。你所要做的就是傳遞給定的字符串格式作爲兩個參數。返回是以2D陣列添加和移除的客人陣列。我沒有添加if($percent != 100),但我確定您可以將該變量作爲參數傳遞,並在該函數內需要的位置注入該行。

代碼很受評論,所以希望它會很清楚。

// 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 
} 

輸出

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"} } 

編輯!

誤讀問題並進行編輯,現在它返回二維數組中的兩個數組。