简体   繁体   中英

Multidimensional Array - Get unique values, but count all duplicates

I have a multi-dimensional array that I would like to get unique sub-values from, but also have a count of how many times those unique sub-values occurred.

For instance, this would be my starting array:

[0] => Array
        (
            [0] => Array
                (
                    [id] => 1533438473619168
                )
            [1] => Array
                (
                    [id] => 3333333333333333
                )
        )
 [1] => Array
        (
            [0] => Array
                (
                    [id] => 1533438473619168
                )
            [1] => Array
                (
                    [id] => 5555555555555555
                )
        )
 [2] => Array
        (
            [0] => Array
                (
                    [id] => 1533438473619168
                )
            [1] => Array
                (
                    [id] => 77777777777777777
                )
        )

In the end, I'd like to have an array that looks like this:

[0] => Array
        (
            [0] => Array
                (
                    [id] => 1533438473619168
                    [count] => 3
                )
            [1] => Array
                (
                    [id] => 3333333333333333
                    [count] => 1
                )
            [2] => Array
                (
                    [id] => 5555555555555555
                    [count] => 1
                )
            [3] => Array
                (
                    [id] => 77777777777777777
                    [count] => 1
                )
        )

Is there any general/easy way to do this without iterating through the first array for each value, comparing/storing the values in a temporary array, checking them, and adding to the count?

To get this exact format you may need to iterate thought your current array and do the counting manually, however php has the array_count_values() and array_unique() functions for this kind of thing:

http://php.net/manual/en/function.array-count-values.php

http://php.net/manual/en/function.array-unique.php

Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. Note that a reference to the output array $counted is used in the callback.

array_walk_recursive($ids, function($id, $k) use (&$counted) {
    $counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});

Using the id as the key in the $counted array will simplify the counting. The result of this will be somewhat different from your suggested output, but in my opinion it would actually be simpler to use. (eg foreach ($counted as $id => $count) {... ).

$counted = array(
    "1533438473619168" => 3
    "3333333333333333" => 1
    "5555555555555555" => 1
    "77777777777777777" => 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM