0

I have two array

$a = [500,0,300,0];
$b = [0, 100, 0, 100];

My desire output :

$c = [500,400,700,600];

I have tried by below approach, but not getting desire output.

<?php 

function get_c()
{
    $amount = 0;
    
    if (func_get_arg(0) > 0) {
        $amount = $amount + func_get_arg(0);
    } else {
        $amount = $amount - func_get_arg(1);
    }
    return $amount;

}
$a = [500,0,300,0];
$b = [0, 100, 0, 100];


$c = array_map(
    'get_c',
    $a,
    $b
);

print_r($c);

Getting output [500,-100,300,-100] . I understood my amount value always getting zero in if condition. How can I solve this problem ?

Niloy Rony
  • 602
  • 1
  • 8
  • 23
  • Could you please exaplain why `$c[1]` is `400`? `0` + `100` should be `100`? – 0stone0 Apr 10 '21 at 17:06
  • Ohhhh I think I get it now. You want to sum all previous entries in both arrays. – Liftoff Apr 10 '21 at 17:08
  • @David yes you are right. It's like transaction statement. – Niloy Rony Apr 10 '21 at 17:08
  • @0stone0 It's like transactions , not summation two arrays. – Niloy Rony Apr 10 '21 at 17:09
  • That's not how [map](https://en.m.wikipedia.org/wiki/Map_%28higher-order_function%29) functions. Read the documentation on [`array_map`](http://php.net/array_map) for PHP's implementation. Also check out the related operations: filter, fold/reduce and zip/convolution. – outis Apr 10 '21 at 17:31

3 Answers3

1

You can fix it with a simple foreach loop following your map. This loop just sums the whole result array in order.

foreach($c as $i => $val)
{
    if($i == 0)
        continue;
        
    $c[$i] = $c[$i-1] + $val;
}

Full code:

function get_c()
{
    $amount = 0;
    
    if (func_get_arg(0) > 0) {
        $amount = $amount + func_get_arg(0);
    } else {
        $amount = $amount - func_get_arg(1);
    }
    return $amount;

}

$a = [500,0,300,0];
$b = [0, 100, 0, 100];


$c = array_map(
    'get_c',
    $a,
    $b
);

foreach($c as $i => $val)
{
    if($i == 0)
        continue;
        
    $c[$i] = $c[$i-1] + $val;
}

print_r($c);

Result:

Array
(
    [0] => 500
    [1] => 400
    [2] => 700
    [3] => 600
)
Liftoff
  • 24,717
  • 13
  • 66
  • 119
1

We can use a simple for loop if the both array have the same indexes.

Use (after the first iteration (??)) our last value in $c to get the new one:

<?php

$a = [500,0,300,0];
$b = [0, 100, 0, 100];
$c = [];

for ($i=0; $i < sizeof($a); $i++) {
    $c[$i] = ($c[$i - 1] ?? 0) + $a[$i] - $b[$i];
}

var_dump($c);
array(4) {
  [0]=>
  int(500)
  [1]=>
  int(400)
  [2]=>
  int(700)
  [3]=>
  int(600)
}

Try it online!

0stone0
  • 34,288
  • 4
  • 39
  • 64
1

when you use array_map function it calls get_c for every element and initializes $amount =0 each time. to solve this problem you can just use a simple foreach loop.

<?php 

function get_c($a, $b) :array
{
     $amount = 0;
     $output = array();
    
    foreach($a as $key =>$value) 
    {
        if($value >0) {
             $amount = $amount + $value;
        }
        else $amount = $amount - $b[$key];
        
        $output[] = $amount;
    }
    return $output;
}

$a = [500,0,300,0];
$b = [0, 100, 0, 100];


$c = get_c($a,$b);
print_r($c);

I hope that answers your question