Porting a C ++ map using std :: accumulate to PHP

I'm not very good at manipulating arrays in PHP, so I have a simple porting question. In C ++, I have a map std::map<int, int>for which implicit key ordering is an important part of the structure. What I want to do is summarize all the values ​​for the initial key range, which I really like:

// 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 the idiomatic way of writing this data structure and battery into PHP?

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

To explain the accumulation: the function accumulatesums up all the values ​​of the card, the keys of which are no more N. For example, take this map:

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

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


Update: I just realized that there really is no reason why each timestamp should appear only once, so there should be a data structure std::multimap<int, int>. One and the same accumulation function works verbatim, but if it takes time for a PHP solution to be an array key, this will no longer work. But this is not strictly important; I believe that a solution in which each time is required to be unique will be enough.

+5
3

, / PHP , . PHP key => value, , , . - :

$map = array(1 => 20, 2 => 30, 3 => -10, ...);
$n = 3;

$result = 0;
foreach ($map as $key => $value) {
    if ($key <= $n) {
        $result += $value;
    }
}

, , :

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

:

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

++- , PHP , :

$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);
});
+3

++, PHP, .

PHP . ++, , - .

, :

$arr = array();

, . -, [] . ( int key + 1), . :

$arr[] = 1;
$arr[] = 2;
$arr[] = 4;

$arr /:

0 => 1
1 => 2
2 => 4

:

$arr[42] = 'cool';
$arr['foo'] = 'bar';

:

0 => 1
1 => 2
2 => 4
42 => 'cool'
'foo' => 'bar'

, PHP . , ( , ), , , , .. , PHP foreach . :

foreach ($arr as $key => $value) {
    echo 'Value of key ' . $key . ' is ' . $value;
}

, :

,

: , . , , :

$sum = array_sum(array_slice($arr, 0, $n));

, !

+5

, theres array_reduce, - . :

$result = array_reduce($your_array, function($a, $b) { return $a + $b; });

. array_sum (, ).

+2
source

All Articles