2014-02-26 141 views
0

我有一个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中的每个阵列都是等效的

如果是这样,我想返回一个副本它,否则我想返回一个空数组。

我知道我可以比较数组2在2 foreach循环或类似的东西,但有没有一个正确的方法来实现这一目标? 我不知道,我可以用array_map()应用一种递归函数吗?

回答

0

试试这个:

function checkArray($schema){ 
    $arr = array_unique($schema); 
    if(count($arr) == 1){ 
    return $arr; 
    } 
    else{ 
    return array(); 
    } 
} 
0

试试这个

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(); 
} 

Demo

0
function emptycheck($schema) { 
    $schema=array_unique($schema); 
    if(count($schema)=!1){ 
     $schema=array();//empty array 
    } 
    return $schema; 
} 
0

最简单的方法就是把它们direcly比较 - 即

$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. 

否则,您必须使用某些差异的方法(也有与像array_udiff回调) 或写自己的方法对它们进行比较。