Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:43:50+00:00 2026-06-11T00:43:50+00:00

i’m trying to figure out how to sum certain values of a multi-dimensional array

  • 0

i’m trying to figure out how to sum certain values of a multi-dimensional array if they have similar dates.

Here’s my array:

<?$myArray=array(

array(
        'year' => 2011,
        'month ' => 5,
        'day' => 13,
        'value' => 2
    ),
array(
        'year' => 2011,
        'month '=> 5,
        'day' => 14,
        'value' => 5
    ),
array(
        'year' => 2011,
        'month ' => 5,
        'day' => 13,
        'value' => 1
    ),
array(
        'year' => 2011,
        'month ' => 5,
        'day' => 14,
        'value' => 9
    )
);?>

here’s how i’d like the output to look:

<?$output=array(

array(
        'year' => 2011,
        'month ' => 5,
        'day' => 13,
        'value' => 3 //the sum of 1+2
    ),
array(
        'year' => 2011,
        'month '=> 5,
        'day' => 14,
        'value' => 14 //the sum of 5+9
    )
);?>

Notice how the 4 sub-arrays were matched on year/month/day and then only the value was summed. I’ve seen other SO threads on this topic but can’t find one where only the value is summed and not the year/month/day values too.

Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:43:52+00:00Added an answer on June 11, 2026 at 12:43 am

    It may be easiest to initially index your output array with a combination of the year/month/day:

    Note: Your example array above has all its month keys with a trailing space. I’m just using month here with no trailing space.

    // Initialize output array...
    $out = array();
    
    // Looping over each input array item
    foreach ($myArray as $elem) {
      // Initialize a new element in the output keyed as yyyy-mm-dd if it doesn't already exist
      if (!isset($out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']])) {
        $out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']] = array(
          // Set the date keys...
          'year' => $elem['year'],
          'month' => $elem['month '],
          'day' => $elem['day'],
          // With the current value...
          'value' => $elem['value']
        );
      }
      // If it already exists, just add the current value onto it...
      else {
         $out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']]['value'] += $elem['value'];
      }
    }
    
    // Now your output array is keyed by date.  Use array_values() to strip off those keys if it matters:
    $out = array_values($out);
    

    Outputs (before calling array_values()):

    array(2) {
      '2011-5-13' =>
      array(4) {
        'year' =>
        int(2011)
        'month' =>
        int(5)
        'day' =>
        int(13)
        'value' =>
        int(3)
      }
      '2011-5-14' =>
      array(4) {
        'year' =>
        int(2011)
        'month' =>
        int(5)
        'day' =>
        int(14)
        'value' =>
        int(14)
      }
    }
    

    Update:

    To do the same thing with single-key dates (rather than 3-parts) it is easier without the concatenation:

    $myArray=array(
          array(
                'date' => '2011-05-13',
                'value' => 2
            ),
        array(
                'date' => '2011-05-14',
                'value' => 5
            ),
            array(
                'date' => '2011-05-13',
                'value' => 7
            ),
        array(
                'date' => '2011-05-14',
                'value' => 3
            ),
    
    );
    
       foreach ($myArray as $elem) {
          // Initialize a new element in the output if it doesn't already exist
          if (!isset($out[$elem['date']])) {
            $out[$elem['date'] = array(
              // Set the date keys...
              'date' => $elem['date'],
              // With the current value...
              'value' => $elem['value']
            );
          }
          else {
            $out[$elem['date']]['value'] += $elem['value'];
          }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.