fork download
  1. <?php
  2.  
  3. function subtrai_array( $arr1, $arr2 ) {
  4. foreach ( $arr2 as $chave => $valor ) {
  5. if( array_key_exists( $chave, $arr1 ) ) {
  6. $arr1[$chave] = $arr1[$chave] - $valor;
  7. } else {
  8. $arr1[$chave] = -$valor;
  9. }
  10. }
  11. return $arr1;
  12. }
  13.  
  14. $a1 = Array (
  15. 'bife' => 3,
  16. 'batata' => 2,
  17. 'arroz' => 1,
  18. 'zóião' => 3
  19. );
  20. $a2 = Array (
  21. 'bife' => 2,
  22. 'batata' => 2,
  23. 'couve' => 1
  24. );
  25. print_r( subtrai_array( $a1, $a2 ) );
  26. ?>
Success #stdin #stdout 0.02s 20568KB
stdin
Standard input is empty
stdout
Array
(
    [bife] => 1
    [batata] => 0
    [arroz] => 1
    [zóião] => 3
    [couve] => -1
)