[英]Compare multiple arrays
我有一个2维数组,其中包含多个数组,如下所示:
$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();
}
尝试这个 :
function checkArray($schema){
$arr = array_unique($schema);
if(count($arr) == 1){
return $arr;
}
else{
return array();
}
}
function emptycheck($schema) {
$schema=array_unique($schema);
if(count($schema)=!1){
$schema=array();//empty array
}
return $schema;
}
最简单的方法是直接比较它们-即
$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.
否则,您必须使用某些diff方法(与array_udiff之类的回调一起使用)或编写自己的方法进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.