如何检查mutidimension数组中的值是否相同?

时间:2017-06-23 09:14:37

标签: php if-statement multidimensional-array

如果我有像这样的多维数组

Array
(
    [0] => Array
        (
            [value] => Dummy
            [key] => 5501____I2-6
        )

    [1] => Array
        (
            [value] => Dummy
            [key] => 5497____I2-2
        )

    [2] => Array
        (
            [value] => Dummy
            [key] => 5502____I2-7
        )

    [3] => Array
        (
            [value] => Dummy
            [key] => 5499____I2-4
        )

    [4] => Array
        (
            [value] => Dummy
            [key] => 5496____I2-1
        )

    [5] => Array
        (
            [value] => Dummy
            [key] => 5500____I2-5
        )

    [6] => Array
        (
            [value] => Dummy
            [key] => 5503____I2-8
        )

    [7] => Array
        (
            [value] => Dummy
            [key] => 5498____I2-3
        )

)

如何检查 [value] => 中的每个值是否相同?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

对于二维数组,您可以通过以下方式检查重复值。

$array = array();
$temp_array=array();
$array[0]['value']='Dummy';
$array[0]['key']='1';
$array[1]['value']='Dummy';
$array[1]['key']='2';
$array[2]['value']='Not Dummy';
$array[2]['key']='3';

foreach($array as $k=>$step1){
    if(in_array($step1['value'],$temp_array)){
        // In this you can find out duplicate values.
    }
    $temp_array[$k]=$step1['value'];
}

答案 1 :(得分:0)

感谢大家的帮助。

我现在找到了修复它的方法。

我不确定这是最好的方法,但它可以为我解决这个问题。

$data = Array
(
    [0] => Array
        (
            [value] => Dummy
            [key] => 5501____I2-6
        )

    [1] => Array
        (
            [value] => Dummy
            [key] => 5497____I2-2
        )

    [2] => Array
        (
            [value] => Dummy
            [key] => 5502____I2-7
        )

    [3] => Array
        (
            [value] => Dummy
            [key] => 5499____I2-4
        )

    [4] => Array
        (
            [value] => Dummy
            [key] => 5496____I2-1
        )

    [5] => Array
        (
            [value] => Dummy
            [key] => 5500____I2-5
        )

    [6] => Array
        (
            [value] => Dummy
            [key] => 5503____I2-8
        )

    [7] => Array
        (
            [value] => Dummy
            [key] => 5498____I2-3
        )

)

$newarray = array();
  for ($i=0; $i < count($data); $i++) {
     array_push($newarray,$json_data[$i]['value']);
}
if (count(array_unique($newarray)) == 1){
   echo 'The same';
 }
else{
   echo 'not The same';
}