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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:09:41+00:00 2026-05-25T11:09:41+00:00

I’m not terribly well versed with array manipulation in PHP, so I have a

  • 0

I’m not terribly well versed with array manipulation in PHP, so I have a simple porting question. In C++ I have a map std::map<int, int>, for which the implicit ordering on the key is a crucial part of the structure. What I want to do is to sum up all the values for an initial range of keys, which I do like this:

// accumulate helper, since "value_type" is "pair<int, int>"
int pair_adder(int n, const std::map<int, int>::value_type & p) { return n + p.second; }

// To add up values for keys up to N:
int total_value_up_to_time_N(int N)
{
  return std::accumulate(mymap.begin(), mymap.upper_bound(N), 0, pair_adder);
}

What would be an idiomatic way to write this data structure and accumulator in PHP?

To explain the context: The data structure is a simple time series, and I want to know how much I have accumulated at time N. The C++ map is always sorted by key, so I can add elements mymap[time] = value; in any order, and the map always contains the elements in time order.

To explain the accumulation: The accumulate function sums up all the map’s values whose keys are no greater than N. For example, take this map:

 mymap = { { 1, 20}, {2, 30}, {3, -10}, {4, 15} };

Then for N = 2 I accumulate 50, for N = 3 I accumulate 40, and for N = 12 I accumulate 55.


Update: I just realized that actually there’s no reason why each timestamp should occur only once, so the data structure should really be a std::multimap<int, int>. The same accumulation function works verbatim, but if the PHP solution requires the time to be an array key, than that would no longer work. But this is not strictly important; I believe a solution in which each time is required to be unique will suffice.

  • 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-05-25T11:09:42+00:00Added an answer on May 25, 2026 at 11:09 am

    Unfortunately, the PHP array map/reduce functions only map over values, not keys. If your map is using PHP key => value arrays, you’ll need some tricks to filter out the values whose keys have a certain value. The easiest is a straight forward loop:

    $map = array(1 => 20, 2 => 30, 3 => -10, ...);
    $n = 3;
    
    $result = 0;
    foreach ($map as $key => $value) {
        if ($key <= $n) {
            $result += $value;
        }
    }
    

    From here you can of course get creative though:

    $result = array_sum(array_intersect_key(
        $map,
        array_flip(array_filter(array_keys($map), function ($key) use ($n) {
            return $key <= $n;
        }))
    ));
    

    Or:

    $result = array_sum(array_map(function ($key, $value) use ($n) {
        return $key <= $n ? $value : 0;
    }, array_keys($map), $map));
    

    If you’re using a more C++ like map, which in PHP would be an array of arrays, that simplifies the problem:

    $map = array(array('key' => 1, 'value' => 20), array(...), ...);
    
    $result = array_reduce($map, function ($v, $m) use ($n) {
        return $v + ($m['key'] <= $n ? $m['value'] : 0);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.