Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 比较两个文本_Php_Arrays - Fatal编程技术网

Php 比较两个文本

Php 比较两个文本,php,arrays,Php,Arrays,我试图找出每个数组中的哪些元素以及它们之间的区别,但它说所有元素都不在具有in_数组函数的数组中 我需要找到哪些需要删除,哪些需要添加,然后将它们作为数组返回给jquery // prepares array function prep($string){ $separator = "/n"; $string = explode($separator , $string ); $string = array_map('trim', $string); return

我试图找出每个数组中的哪些元素以及它们之间的区别,但它说所有元素都不在具有in_数组函数的数组中

我需要找到哪些需要删除,哪些需要添加,然后将它们作为数组返回给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;
}
//准备数组
函数准备($string){
$separator=“/n”;
$string=explode($separator,$string);
$string=数组映射($trim',$string);
返回$string;
}
//要测试的线路
$lines='guest13/n guest14/n guest16/n';
$lines2='guest13/n guest16/n guest17/n';
//声明存储以供以后使用
$add=array();
$rem=array();
//测试行以查看是否相同
类似的文本($lines,$lines2,$percent);
//声明数组
$linestest=array();
$linestest2=array();
//待测试的过滤器线路
$linestest=准备($lines);
$lines2test=prep($lines2);
如果($percent!=100){
//检查是否需要将其删除
foreach($linestest作为$line=>$mow){
if(!in_数组('$mow',$linestest2,true)){
$rem[]=$mow;
回显“
删除“$mow; } } foreach($lines2test作为$sigh=>$wow){ if(!in_数组('$wow',$linestest,true)){ $add[]=$wow; echo'
添加'$wow; } //相互呼应 } foreach($rem作为$new){ $mows.=“删除此项”。$new; } foreach($add as$new){ $mows.=“添加此项”。$new; } 回显“
字符串结尾“$mows; }
看来你有几个错误。我删除了您的评论并添加了我自己的评论,以便您更好地理解我所做的更改:

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;
}
如果您想在不使用循环的情况下以较短的方式完成此操作,以下是一个精简版本:

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

我几乎把所有的东西都放在了一个函数中,并且调整了一些部分,使它更清晰。您所需要做的就是传递作为两个参数给定的字符串格式。return是要在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
}
编辑

误读问题并进行编辑,因此它现在返回二维数组中的两个数组

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