[英]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.