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

  • Home
  • SEARCH
  • 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 9015133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:40:30+00:00 2026-06-16T03:40:30+00:00

Ok, I have a JSON string: [{ Name: Title 1, Count1: 556, Count2: 5,

  • 0

Ok, I have a JSON string:

[{
"Name": "Title 1",
"Count1": 556,
"Count2": 5,
"Date": "2012-12-05"
}, {
"Name": "Title 2",
"Count1": 10,
"Count2": 100,
"Date": "2012-12-05"
}, {
"Name": "Title 3",
"Count1": 798,
"Count2": 11,
"Date": "2012-12-04"
}...

and I run json_decode on that, get an array, and now I want to run through that array and calculate totals for Count1 and Count2 for each date… (and the dates can go on for whatever range). What’s the fastest/niftiest way of doing that?

Sort of loop through each key => val par in a foreach, and then somehow group the date keys into a new key in a new array and add the totals in there, so that I’m getting this as an output, in JSON feed at the very end, after json_encode, sorted by date:

[{
"Date": "2012-12-05"
"TotalCount1": 566,
"TotalCount2": 105,
}, {
"Date": "2012-12-04"
"TotalCount1": 798,
"TotalCount2": 11,
}...

how would I group the array values like that before sending the whole array to json_encode?

  • 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-16T03:40:31+00:00Added an answer on June 16, 2026 at 3:40 am

    I think using a $totals array indexed on your date would work here (if I understand you correctly). Something like the example below where $data is your decoded associative JSON array:

    $totals = array();
    
    foreach ($data as $row) {
        if (isset($totals[$row['Date']]) ) {
    
            $totals[$row['Date']]['TotalCount1']
                = $totals[$row['Date']]['TotalCount1'] + $row['Count1'];
    
            $totals[$row['Date']]['TotalCount2']
                = $totals[$row['Date']]['TotalCount2'] + $row['Count2'];
    
        } else {
    
            $totals[$row['Date']] = array(
                 'Date' => $row['Date'],
                 'TotalCount1' => $row['Count1'],
                 'TotalCount2' => $row['Count2'],
            );
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JSON String { name:Product, properties: { id: { type:number, description:Product
I have a JSON string as the following; { 1: { name: Jerry, age:
I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON ) { name:Product, properties: {
I have the following almost JSON string: <?php $string = '{0:{id:1,name:Antiek en Kunst,subs:[{id:2,name:Antiek |
I have JSON string that has nested objects with dynamic names that vary each
I have a JSON string that I would like to include as a value
I have the following json output when i call a sample webservice <string>[{Name:Ajay Singh,Company:Birlasoft
I have a json like below- String sdata = [{ name : AAPL, data:[[1112832000000,43.56],[1112918400000,43.74],[1113177600000,41.92],[1113264000000,null],[1113350400000,null]],
I have a json string like this: { d: { results: [ { __metadata:
I have a json string saved in my db. When i retrieve it from

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.