在PHP数组中找到不同的值

时间:2022-01-07 03:07:24

I have an array that I would like to return only the different value. Ex.:

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

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
        )

)

I would like to get only the item:

我想只得到这个项目:

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

Any ideas?

2 个解决方案

#1


2  

try this, check the 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)]);

#2


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

#1


2  

try this, check the 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)]);

#2


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