在PHP数组中找到不同的值

时间:2017-05-25 07:21:45

标签: php arrays multidimensional-array

我有一个数组,我想只返回不同的值。例:

Array
(
    [555] => Array
        (
            [123] => 2017-02-04 00:00:00
            [124] => 2017-02-04 00:00:00
            [125] => 2017-08-31 14:59:59
            [126] => 2017-02-04 00:00:00
            [127] => 2017-02-04 00:00:00
        )

)

我想只得到这个项目:

[125] => 2017-08-31 14:59:59

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

试试这个,查看live demo

$count = array_count_values($array[555]);
asort($count);     //sort according the times of value. 
print_r([array_search(key($count), $array[555]) => key($count)]);

答案 1 :(得分:0)

<?php


$test =Array
(
    555 => Array
    (
        123 => '2017-02-04 00:00:00',
        124 => '2017-02-04 00:00:00',
        125 => '2017-08-31 14:59:59',
        126 => '2017-02-04 00:00:00',
        127 => '2017-02-04 00:00:00'
    )

);
foreach ($test as $k => $v) {
    $res = array_count_values($v);
}
$fin = '';
foreach ($res as $k => $v) {
    if ($v == 1) {
        $fin = $k;
    }
}
print_r($fin);