比较多维数组值(整数/浮点数)

时间:2014-07-07 07:56:25

标签: php arrays if-statement multidimensional-array compare

首先,抱歉我的英语不好 我在比较多维数组值时遇到了问题

这是我要解决的代码:

$bla1 = array( array(0.1,0.5), array(0.1,0.5) )
$bla2 = array( array(0.5,0.1), array(0.5,0.1) )
$bla3 = array( array(0.1,0.5), array(0.5,0.1) )    

  if(check_every_single_values_in_array($bla1){
      //Any command in here will not be executed / return false
      }
  if(check_every_single_values_in_array($bla2){
      //Any command in here will not be executed / return false
      }
  if(check_every_single_values_in_array($bla3){
      //Any command in here will not be executed / return false
      }

任何类型杂耍和不同的值都不会执行任何命令 否则,如果没有任何类型杂耍和不同的值,它将执行命令:

$bla4 = array( array(0.5,0.5), array(0.5,0.5) )
$bla5 = array( array(1,1), array(1,1) )

  if(check_every_single_values_in_array($bla4){
     //Any command in here will be executed / return true
     }
  if(check_every_single_values_in_array($bla5){
     //Any command in here will be executed / return true
     }

我试图用array_diff或一些逻辑和算术运算符来解决它 他们都没有工作 我的问题是,如何比较多维数组中的所有值?什么是最好,最短,最快的方法来解决它?

1 个答案:

答案 0 :(得分:1)

或者,您可以使用serialize()

执行此类操作
$bla1 = array(array(0.35,0.5), array(0.35,0.5));
$bla2 = array(array(103.5,0.1), array(103.5,0.1));
$bla3 = array(array(0.1,0.5), array(0.5,0.1));

function check_every_single_values_in_array($array) {
    $same = true;
    $check = array_map('unserialize', array_unique(array_map('serialize', $array)));
    if(count($check) > 1) {
        $same = false;
    }
    return $same;
}

if(check_every_single_values_in_array($bla1)) {
    echo 'Every values on bla1 is same <br/>';
}
if(check_every_single_values_in_array($bla2)) {
    echo 'Every values on bla2 is same <br/>';
}
if(check_every_single_values_in_array($bla3)) {
    echo 'Every values on bla3 is same';
} else {
    echo 'Every values on bla3 is not same';
}