10

I have searched this question a lot. But I could not find a proper solution anywhere. Just like you do an array_count_values() for a single dimensional array, what do you do for a multi dimensional array if you want similar type of a solution?

For example-

Array
(
    [0] => Array
        (
            [07/11] => 134
        )

    [1] => Array
        (
            [07/11] => 134
        )

    [2] => Array
        (
            [07/11] => 145
        )

    [3] => Array
        (
            [07/11] => 145
        )

    [4] => Array
        (
            [07/12] => 134
        )

    [5] => Array
        (
            [07/12] => 99
        )
)

The output that I want is-

Date: 07/11, ID: 134, Count: 2
Date: 07/11, ID: 145, Count: 2
Date: 07/12, ID: 135, Count: 1
Date: 07/12, ID: 99, Count: 1

How do I do this?

2
  • 2
    You could either run a nested foreach loop or create a recursive function to navigate multi-dimensions Commented Jul 14, 2013 at 18:44
  • I figured out I need to use a nested loop to navigate multi-dimensions but I am not able to figure out how will I count unique IDs for each date. Commented Jul 14, 2013 at 18:56

4 Answers 4

16

Using the variable $arr for your array, you could do this:

$out = array();
foreach ($arr as $key => $value){
    foreach ($value as $key2 => $value2){
        $index = $key2.'-'.$value2;
        if (array_key_exists($index, $out)){
            $out[$index]++;
        } else {
            $out[$index] = 1;
        }
    }
}
var_dump($out);

Output:

Array
(
    [07/11-134] => 2
    [07/11-145] => 2
    [07/12-134] => 1
    [07/12-99] => 1
)

Here's another version that produces it as a multidimensional array:

$out = array();
foreach ($arr as $key => $value){
    foreach ($value as $key2 => $value2){
        if (array_key_exists($key2, $out) && array_key_exists($value2, $out[$key2])){
            $out[$key2][$value2]++;
        } else {
            $out[$key2][$value2] = 1;
        }
    }
}

Output:

Array
(
    [07/11] => Array
        (
            [134] => 2
            [145] => 2
        )

    [07/12] => Array
        (
            [134] => 1
            [99] => 1
        )

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

1 Comment

The second solution was amazing. I never thought this could be done this way. Thanks, so much I learned :)
2
<?php
  $array = array(array('07/11' => '134'), array('07/11' => '134'), array('07/12' => '145'));
  $count = array();
  foreach ($array as $val) {
    foreach ($val as $key => $subval) {
      $count[$key]++;
    }
  }
  print_r($count);

4 Comments

Hi, thanks for the answer. But your solution will just give me count of unique IDs. I want it date wise.
Oops... See last revision (sorry about that). :)
beautiful solution. +1
Your new solution gives me the count of unique date only. Not the IDs.
0

In your place I would have altered the structure of the data array. In your case:

Array( 
    [07/11] => Array
        (
            [0] => 134,
            [1] => 134,
            [2] => 145,
            [3] => 145,
            ...
        )
)

2 Comments

Interesting. How do I proceed after doing this?
Also, this is not always a viable solution as many times one will need to work with an already fixed data..
0

Smart way, We know that array_count_values(): Counts all the values of an array

Just like you do an array_count_values() for a single dimensional array, what do you do for a multi dimensional array.

We do it by turning it to one single dimensional array, then we call our funciton

$input = array(
    array('07/11' => '134'),
    array('07/11' => '134'), 
    array('07/11' => '145'),
    array('07/11' => '145'),
    array('07/12' => '134'), 
    array('07/12' => '99')
    );
$output = [];
foreach ($input as $inner){
    foreach ($inner as $key => $value){
        $output[] = "Date: $key, ID: $value, Count:";
    }
}

OUTPUT: as you expect

foreach(array_count_values($output) as $key => $value) {
    echo "$key $value<br>";
}

/*Date: 07/11, ID: 134, Count: 2
Date: 07/11, ID: 145, Count: 2
Date: 07/12, ID: 135, Count: 1
Date: 07/12, ID: 99, Count: 1*/

Updated for cases where there are multiple elements in each sub-array

   $input = array(
    array('07/11' => '134', '07/12' => '100'),
    array('07/11' => '134', '07/12' => '100'), 
    array('07/11' => '145', '07/12' => '200'),
    array('07/11' => '145', '07/12' => '200'),
    array('07/12' => '134', '07/13' => '300'), 
    array('07/12' => '99', '07/13' => '400')
    );
$output = [];
foreach ($input as $inner){
    if (count($inner) > 1) {
        // handle multiple elements in sub-array
        foreach ($inner as $key => $value){
            if (isset($output[$key][$value])) {
                $output[$key][$value]++;
            } else {
                $output[$key][$value] = 1;
            }
        }
    } else {
        // handle single element in sub-array
        foreach ($inner as $key => $value){
            $output[] = "Date: $key, ID: $value, Count:";
        }
    }
}

if (count($output) > 1) {
    // handle multiple elements in output array
    foreach ($output as $date => $counts) {
        foreach ($counts as $id => $count) {
            echo "Date: $date, ID: $id, Count: $count<br>";
        }
    }
} else {
    // handle single element in output array
    foreach(array_count_values($output) as $key => $value) {
        echo "$key $value<br>";
    }
}

Result would be:

Date: 07/11, ID: 134, Count: 2
Date: 07/11, ID: 145, Count: 2
Date: 07/12, ID: 100, Count: 2
Date: 07/12, ID: 134, Count: 1
Date: 07/12, ID: 99, Count: 1
Date: 07/13, ID: 300, Count: 1
Date: 07/13, ID: 400, Count: 1

3 Comments

this will not work if there are multiple elements in each sub array
check it now, for cases where if there are multiple elements in each sub-array!
looks wrong. I think it will not count unique rows in multidimensional array.

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.