0

First pardon me for the title as I couldn't find a proper title for it. Please have a look at the array

Array
(
[0] => Array
    (
        [0] => 2017
        [1] => Array
            (
                [0] => December
            )

    )

[1] => Array
    (
        [0] => 2017
        [1] => Array
            (
                [0] => December
            )

    )

[2] => Array
    (
        [0] => 2017
        [1] => Array
            (
                [0] => October
            )

    )

[3] => Array
    (
        [0] => 2016
        [1] => Array
            (
                [0] => December
            )

    )

 )

You can see 2017 is duplicated three times and inside 2017 December is duplicated two times. Now I want to get a multi dimensional array from this array that will show the occurrence of 2017 and it's month.

Something like

Array
(
  [2017] => 3
  [December]=> 2 // should be a nested array of 2017
  [October]=> 1 // should be a nested array of 2017
  [2016] => 1
  [December]=> 1 // should be a nested array of 2016
)

I tried array_count_values and some more custom code but all I managed to get

Array
(
[2017] => 3
[2016] => 1
)

Edit: The months count doesn't has be keyed like this. All I need to know year occurrence and month occurrence under the year

Any help is highly appreciated. Thanks.

4
  • 1
    Two [December] key on your desired result is not possible Commented Dec 22, 2017 at 2:30
  • 1
    Your expected results is not valid syntax. If you want the months as an nested array under the year, the count also needs to have a key. Commented Dec 22, 2017 at 2:31
  • Hi @Erwin it doesn't has to be keyed like this, all I need the month occurrence under the year. Commented Dec 22, 2017 at 2:38
  • Hi @Sean I didn't get this "the count also needs to have a key" Commented Dec 22, 2017 at 2:38

1 Answer 1

3

You can try to loop then check if year key and month key already exist

$group = [];
foreach ($array as $value) {
    $month = $value[1][0];
    if (!isset($group[$value[0]])) {
        $group[$value[0]] = array('count' => 0);
    }
    if (!isset($group[$value[0]][$month])) {
        $group[$value[0]][$month] = 0;
    }
    $group[$value[0]][$month] += 1;
    $group[$value[0]]['count'] += 1;
}

print_r($group);

If you don't necessarily need the count, you can change the first if condition's execution to $group[$value[0]] = array(); and the line $group[$value[0]]['count'] += 1;

foreach ($array as $value) {
    $month = $value[1][0];
    if (!isset($group[$value[0]])) {
        $group[$value[0]] = array();
    }
    if (!isset($group[$value[0]][$month])) {
        $group[$value[0]][$month] = 0;
    }
    $group[$value[0]][$month] += 1;
}

print_r($group);
Sign up to request clarification or add additional context in comments.

1 Comment

Just like a boss! :) Thank you boss for such a nice solution!

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.