-3

I have a 5 level nested array and I want to sort it in a custom way.

After some sorting the structure is:

[shortcode1] => Array
    (
        [country1] => Array
            (
                [count] => 10
                [revenue_value] => 11
            )

    )

[shortcode2] => Array
    (
        [country1] => Array
            (
                [count] => 24
                [revenue_value] => 52
            )

    )
[shortcode3] => Array
    (
        [country2] => Array
            (
                [count] => 25
                [revenue_value] => 52
            )

    )

The result I want is to group the array by country and have it like:

[country1] => Array
(
    [count] => 34            // sum of all counts of country1
    [revenue_value] => 63    // sum of all revenue_values of country1
)
[country2] => Array
(
    [count] => 25            // sum of all counts of country2
    [revenue_value] => 52    // sum of all revenue_values of country2
)

This is my code so far:

        <?php foreach ($country_shortcode as $shortcode): 
            $count = 0; 
            $revenue = 0;
            ?>
            <?php foreach ($shortcode as $country=>$value): ?>
                <?php 
                    $count += $value['count'];
                    $revenue += $value['revenue_value'];
                    $country_sum[$country] = array(
                            'count' => $count,
                            'revenue' => $revenue,
                        ); 
                    ?>
            <?php endforeach; ?>
        <?php endforeach; ?>

But it only displays the last value per country

UPDATE + ANSWER:

This is the updated code that does exactly what I wanted to do.

        $country_data;
        <?php foreach ($country_shortcode as $shortcode): 
            $count = 0; 
            $revenue = 0;
            ?>
            <?php foreach ($shortcode as $country=>$value): ?>
                <?php 
                    if (!isset($country_sum[$country])) $country_sum[$country] = array('count' => 0, 'revenue'=>0);
                    $country_sum[$country]['count'] += $value['count'];
                    $country_sum[$country]['revenue'] += $value['revenue_value'];
                ?>
            <?php endforeach; ?>
        <?php endforeach; ?>
2
  • Check similar issue link Commented Sep 23, 2014 at 10:05
  • this is not sorting. It's some kind of re-organizing/re-grouping the arrays. Commented Sep 23, 2014 at 10:12

1 Answer 1

0

It's too easy... just loop over the array creating a new one

foreach ($array as $sub) {
  foreach ($sub as $countryName => $data) {
    if (!isset($result[$countryName])) {
       $result[$countryName] = ['count' => 0, 'revenue_value' => 0];
    }
    $result[$countryName]['count'] += $data['count'];
    $result[$countryName]['revenue_value'] += $data['revenue_value'];
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

the !isset($result[$countryName]) was the thing I was looking for. Thanks man for the help.

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.