Php 比较多个阵列
我有一个二维数组,其中包含多个数组,如下所示:Php 比较多个阵列,php,arrays,Php,Arrays,我有一个二维数组,其中包含多个数组,如下所示: $schema = Array( Array("prestation_1" => 123, "prestataire_1" => 321, "prestation_2" => 456, "prestataire_2" => 654), Array("prestation_1" => 123, "prestataire_1" => 321, "prestation_2" => 456, "pr
$schema = Array(
Array("prestation_1" => 123, "prestataire_1" => 321, "prestation_2" => 456, "prestataire_2" => 654),
Array("prestation_1" => 123, "prestataire_1" => 321, "prestation_2" => 456, "prestataire_2" => 654),
Array("prestation_1" => 123, "prestataire_1" => 321, "prestation_2" => 456, "prestataire_2" => 654),
Array("prestation_1" => 123, "prestataire_1" => 321, "prestation_2" => 456, "prestataire_2" => 654)
)
注意:主数组中的数组始终具有相同的结构(相同的键)
我想知道主数组$schema
中的每个数组是否等效
如果是这样,我想返回它的一个副本,否则我想返回一个空数组
我知道我可以在foreach循环或类似的东西中比较数组2乘2,但有没有合适的方法来实现这一点?
我不知道,一种递归函数,我可以用array_map()应用?试试这个
function check( $array)
{
$array=array_values($array);
$k=0;
for($i=0;$i<count($array);$i++)
{
if(count($array)-1 > $i)
{
if($array[$i] == $array[$i+1])
$k=0;
else
$k=1;
}
}
if($k == 0)
return $array[0];
else
return array();
}
函数检查($array)
{
$array=数组值($array);
$k=0;
对于($i=0;$i$i)
{
如果($array[$i]==$array[$i+1])
$k=0;
其他的
$k=1;
}
}
如果($k==0)
返回$array[0];
其他的
返回数组();
}
试试这个:
function checkArray($schema){
$arr = array_unique($schema);
if(count($arr) == 1){
return $arr;
}
else{
return array();
}
}
最简单的方法是直接比较它们——即
$schema[0] == $schema[1]; // etc., checking for the same key/value pairs
或
否则,您必须使用一些diff方法(还有类似于array\u udiff的回调)
或者编写自己的方法来比较它们
$schema[0] == $schema[1]; // etc., checking for the same key/value pairs
$schema[0] === $schema[1]; // etc., checking whether key/value pairs, order and types are the same.