PHP: Calculation on 2 Dimensional Array

Yen

I have 2 arrays as below:

array(2) { ["2018-05"]=> string(2) "15" ["2018-06"]=> string(3) "100" }
array(1) { ["2018-05"]=> string(1) "5" } 

I wish to do calculation to find the difference so it will return:

array(2) { ["2018-05"]=> string(2) "10" ["2018-06"]=> string(3) "100" }

As this is a multidimensional array, I'm not sure how to show the "year-month" as the array key, can someone please enlighten me?

Thanks.

Davit Huroyan

Try this code

$a = ["2018-05"=> "15", "2018-06"=> "100" ];
$b = ["2018-05"=> "5"];

$c = $a;

foreach($b as $k=> $i){
    if(array_key_exists($k,$c)){
        $c[$k] = $a[$k] - $b[$k];
    }
    else{
        $c[$k] = 0-$i;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related