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 8371985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:17:58+00:00 2026-06-09T14:17:58+00:00

I need to group the data in my 2d array by the SiteId column

  • 0

I need to group the data in my 2d array by the SiteId column values and then use the TransactionType value within that group as the second level key which need to collect the total Amount for that transaction in the group.

Input:

[
    ['SiteID' => 147, 'Amount' => '500.00',  'TransactionType' => 'Deposit'],
    ['SiteID' => 147, 'Amount' => '500.00',  'TransactionType' => 'Redemption'],
    ['SiteID' => 147, 'Amount' => '1500.00', 'TransactionType' => 'Deposit'],
    ['SiteID' => 147, 'Amount' => '200.00',  'TransactionType' => 'Reload'],
    ['SiteID' => 150, 'Amount' => '100.00',  'TransactionType' => 'Deposit'],
    ['SiteID' => 3,   'Amount' => '500.00',  'TransactionType' => 'Redemption'],
    ['SiteID' => 150, 'Amount' => '200.00',  'TransactionType' => 'Redemption'],
    ['SiteID' => 3,   'Amount' => '500.00',  'TransactionType' => 'Deposit'],
    ['SiteID' => 3,   'Amount' => '200.00',  'TransactionType' => 'Deposit'],
    ['SiteID' => 3,   'Amount' => '200.00',  'TransactionType' => 'Reload'],
    ['SiteID' => 147, 'Amount' => '500.00',  'TransactionType' => 'Redemption']
]

Desired Result:

[
    147 => ['Deposit' => 2000.0, 'Redemption' => 1000.0, 'Reload' => 200.0],
    150 => ['Deposit' => 100.0, 'Redemption' => 200.0],
    3   => ['Redemption' => 500.0, 'Deposit' => 700.0, 'Reload' => 200.0]
]
  • 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-09T14:18:00+00:00Added an answer on June 9, 2026 at 2:18 pm

    please provide a working array fragment in future.

    $transactions = array (
        array( 'SiteID' => 147, 'Amount' => '500.00', 'TransactionType' => 'Deposit' ),
        array( 'SiteID' => 147, 'Amount' => '500.00', 'TransactionType' => 'Redemption'),
        array( 'SiteID' => 147, 'Amount' => '1500.00', 'TransactionType' => 'Deposit' ),
        array( 'SiteID' => 147, 'Amount' => '200.00', 'TransactionType' => 'Reload' ),
        array( 'SiteID' => 150, 'Amount' => '100.00', 'TransactionType' => 'Deposit' ),
        array( 'SiteID' => 3,   'Amount' => '500.00', 'TransactionType' => 'Redemption' ),
        array( 'SiteID' => 150, 'Amount' => '200.00', 'TransactionType' => 'Redemption' ),
        array( 'SiteID' => 3,   'Amount' => '500.00', 'TransactionType' => 'Deposit' ),
        array( 'SiteID' => 3,   'Amount' => '200.00', 'TransactionType' => 'Deposit' ),
        array( 'SiteID' => 3,   'Amount' => '200.00', 'TransactionType' => 'Reload' ),
        array( 'SiteID' =>147, 'Amount' => '500.00', 'TransactionType' => 'Redemption' )
    );
    
    $totals = null;
    
    foreach ($transactions as $t){
        $amount = (float) $t['Amount'];
        if (isset($totals[ $t['SiteID'] ][ $t['TransactionType'] ])){
            $totals[ $t['SiteID'] ][ $t['TransactionType'] ] += (float) $amount;
        } else {
            $totals[ $t['SiteID'] ][ $t['TransactionType'] ] = (float) $amount;
        }
    }
    
    print_r ($totals);
    

    This will produce a result like this:

    Array
    (
        [147] => Array
            (
                [Deposit] => 2000
                [Redemption] => 1000
                [Reload] => 200
            )
    
        [150] => Array
            (
                [Deposit] => 100
                [Redemption] => 200
            )
    
        [3] => Array
            (
                [Redemption] => 500
                [Deposit] => 700
                [Reload] => 200
            )
    
    )
    

    If you can deal with php notice warnings the loop can be shortened to:

    foreach ($transactions as $t){
        $totals[ $t['SiteID'] ][ $t['TransactionType'] ] += (float) $t['Amount'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to group data from my 2d array by one column and sum
I need to group the data in my indexed array of indexed arrays by
I have a skewed data set and I need to do a group by
I need to use group_concat to build a list of comma separated values but
I have got a group of radio buttons that when clicked need to display
This question is geared towards a group of newly hired developers that need to
I need to create a form with the same group of fields (data is
I've got a group of data that looks like this: 001 001 One 001
I need to create a more organized array of data to parse into my
I need to send a group of checkboxes (with some other data as well)

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.