比较两个文本php

时间:2016-11-28 20:38:14

标签: php arrays

我试图找到每个数组中的数据以及它们之间的区别,但它表示所有数组都不在带有in_array函数的数组中。

我需要找到要删除的内容以及需要添加的内容,将每个内容作为数组返回给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;
}

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

我几乎把它全部放在一个功能中,并调整一些部分以使其更清晰。您需要做的就是传递给定的字符串格式作为两个参数。返回是在2D数组中ADD和REMOVE的guest数组。我没有添加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"} } 

修改<!/强>

误读问题并进行编辑,现在它返回2D数组中的两个数组。