Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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,我有一个二维数组,其中包含多个数组,如下所示: $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.