-3

There two arrays:

Array1 ( 
[309] => "100"
[310] => "200"
[311] => "400"
)

Array2 ( 
[309] => "1"
[310] => "2"
[311] => "4"
) 

How to get resulted array with difference of elements from arrays? i.e:

Array3 ( 
    [309] => "99"
    [310] => "198"
    [311] => "396"
    ) 

Thanks!

3
  • 4
    You need help to subtract? Commented Sep 6, 2017 at 12:27
  • Welcome to Stack Overflow. Please take a tour of the site, read How to Ask and how to create a minimal reproducible example. Before posting a question, search the site and make sure a similar question wasn't already answered. Please also note that Stack Overflow is not a coding service. Show what you have tried and where you got stuck to maximize the chances to get help. Commented Sep 6, 2017 at 12:28
  • Take a look at the PHP function array_map(). Or just use a plain foreach loop. Commented Sep 6, 2017 at 12:29

2 Answers 2

0
<?php

$arr1 = array(309=>"100",310=>"200",311=>"400");
$arr2 = array(309=>"1",310=>"2",311=>"4");

$out = array();
foreach($arr1 as $k => $v){
    // Since you need quote so, otherwise no need
    $out[$k] = '"'.($v - $arr2[$k]).'"';
}
print_r($out);

?>

Output :

Array
(
    [309] => "99"
    [310] => "198"
    [311] => "396"
)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

<?php
$a1=array("309"=>"100","310"=>"200","311"=>"400");
$a2=array("309"=>"1","310"=>"2","311"=>"4");
$res = array();
foreach ($a1 as $key => $value) {

   $res[$key] = $a1[$key]-$a2[$key];
}


print_r($res);
?>

Output

 Array ( 
    [309] => "99"
    [310] => "198"
    [311] => "396"
    )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.