0

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?

3
  • I don't understand why this is a multidimensional problem? How does the 222 array come into play? Commented May 25, 2017 at 7:23
  • @domdom I adjusted the text. Commented May 25, 2017 at 7:25
  • array_count_values() would be one option, I guess. Commented May 25, 2017 at 7:29

2 Answers 2

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)]);
Sign up to request clarification or add additional context in comments.

1 Comment

That's roughly how I would've done it too. Always nice to add a tiny bit of explanation though (even if it might be obvious to us).
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.