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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:18:28+00:00 2026-06-05T13:18:28+00:00

I have an array like below, and I want to do the total of

  • 0

I have an array like below, and I want to do the total of values in a specific manner where all values of ADDED_NEW_(.*) {regular expressions}, and similarly other values.
I have only specific vals like ADDED_NEW, ADDED_OLD, and ADD_LATER.

My array is like:

$stats = Array
(
    [ADDED_NEW_2012_06_12] => 16
    [ADDED_OLD_2012_06_12] => 10
    [ADD_LATER_2012_06_12] => 12
    [ADDED_NEW_2012_06_11] => 16
    [ADDED_OLD_2012_06_11] => 10
    [ADD_LATER_2012_06_11] => 12
)

Can you please tell me how can i obtain my result. I don’t know how to add such values using regex in php. Please help.

The output I am expecting is $ADDED_NEW = 32 (i.e. 16+16), $ADDED_OLD = 20 (i.e. 10+10) and $ADD_LATER = 24 (i.e. 12+12)

  • 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-05T13:18:43+00:00Added an answer on June 5, 2026 at 1:18 pm

    I believe you just want to add values of similar keys in which they all start with ADDED_NEW or ADDED_OLD or ADD_LATER I assume so we can just make 3 counters and just match for those in the key and add to the counters.

    I don’t know much PHP but using manuals and my knowledge from Python, this is what I mustered up:

    <?php
    $ADDED_NEW = 0;
    $ADDED_OLD = 0;
    $ADD_LATER = 0;
    foreach ($stats as $key => $value) {
        if (preg_match("ADDED_NEW_.*", $key)) { $ADDED_NEW += $value; }
        if (preg_match("ADDED_OLD_.*", $key)) { $ADDED_OLD += $value; }
        if (preg_match("ADD_LATER_.*", $key)) { $ADD_LATER += $value; }
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of array like below with all numeric values. I want
I have an array which appears like the one below. I want to slice
I have an Array List that I want to output like my example below.
I have a array which contains values like this See below for script sample
I have array like below the page. I want to find array number which
i have an array like below int[] array = new array[n];// n may be
I have a JSON array like below: var jsonArray = [{k1:v1},{k2:v2},{k3:v3},{k4:v4},{k5:v5}] I don't know
I have the array shown below. I would like to have a resultant array
I have an array like below which is generated by parsing a xml url.
I have an array like this (below is the var_dump) array 0 => object(stdClass)[11]

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.