我有两个阵列一个输出
$a = ( [0] => a [1] => b [2] => b [3] => a )
和另一个输出
$b = ( [0] => dog [1] => cat [2] => cat [3] => dog )
如何比较两个阵列中的模式。
答案 0 :(得分:1)
假设你有这三个数组:
$a = array(0 => 'a',1 => 'b',2 => 'a',3 => 'b');
$b = array(0 => 'a',1 => 'b',2 => 'a',3 => 'b');
$c = array(0 => 'dog',1 => 'cat',2 => 'cat',3 => 'dog');
您可以使用==
运算符
$a == $b; //return TRUE
$a == $c; //return FALSE
if($a == $b){//will return TRUE
//$a has the same values as $b
}
else{
//$a is different than $b
}
答案 1 :(得分:1)
<?php
$a = array( 0 => 'a' ,1 => 'b', 2 => 'b', 3 => 'a' );
$b = array( 0 => 'dog', 1 => 'cat' ,2 => 'cat' ,3 => 'dog' );
$flagA=0;
$flagB=0;
$pattern=array();
$i=0;
foreach ($a as $array){
if($array=='a'){
$pattern[$i]=1;
$flagA++;
}
else{
$pattern[$i]=0;
$flagB++;
}
$i++;
}
print_r($pattern);
$flagarrayB=0;
$flagarrayC=0;
$testingPatern=array();
$i=0;
foreach ($b as $arrayB){
if($arrayB=='dog'){
$testingPatern[$i]=1;
$flagarrayB++;
}
else{
$testingPatern[$i]=0;
$flagarrayC++;
}
$i++;
}
print_r($testingPatern);
if(serialize($pattern) == serialize($testingPatern)){
echo "Patern is the same";
}
else{
echo "Patern is wrong";
}
if($flagA==$flagarrayB && $flagB==$flagarrayC){
echo "TRUE";
}
else
{
echo "FALSE";
}
如果您尝试更改数组$ b中的值,您将看到输出将更改