0

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?

4
  • 1
    No, there's no easier way than that. Commented Mar 31, 2016 at 22:11
  • use id as key for new array to avoid comparing/checking. Then you can remove it using array_values Commented Mar 31, 2016 at 22:15
  • for starters i would merge al your arrays together, then you only have to loop through one array. Commented Mar 31, 2016 at 22:30
  • Thanks for all of your help. I ended up just doing it manually (wasn't hard, as I expected, but was more cumbersome than I had hoped). Commented Apr 2, 2016 at 21:19

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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. (e.g. foreach ($counted as $id => $count) {...).

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

1 Comment

Thanks, I will try this to see if it's more efficient than my current solution but both seem to use a temporary array for storage and the same lines of code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.