简体   繁体   English

多维数组-获取唯一值,但计算所有重复项

[英]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: 为了获得这种精确格式,您可能需要迭代当前数组并手动进行计数,但是php具有针对这种情况的array_count_values()和array_unique()函数:

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

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

Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. 因为您只关心数组的最深值,所以使用array_walk_recursive似乎很适合。 Note that a reference to the output array $counted is used in the callback. 注意,在回调中使用了对输出数组$counted的引用。

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. $counted数组中使用id作为键将简化计数。 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) {... ). (例如foreach ($counted as $id => $count) {... )。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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