相同键的两个数组值相减,并在php中得到匹配和不匹配值的结果

我有两个数组,如 $a 和 $b。我想为相同的键从 $a 减去 $b 。我也想看到两个数组的不匹配键和减法结果。只会减去匹配的键。是否可以不使用 foreach 循环?非强制要求必须无循环求解。但如果没有循环就可能更好。示例数组在下面。


$a=Array

(


    [1] => 4.00

    [2] => 3.00

    [3] => 8.00

    [4] => 4.88

    [5] => 7.88

    [10] => 17.88

)

$b=Array

(

    [1] => 2.00

    [3] => 4.00

    [4] => 2.88

    [7] => 5.00

    [8] => 6.00    

)

我想要这样的结果


$result=array(


[1] => 2.00

[2] => 3.00

[3] => 4.00

[4] => 2.00

[5] => 7.88

[7] => 5.00

[8] => 6.00

[10] => 17.88


);

我的代码是:


$res1=$res2=$res3=array();

foreach( $a as $k1=>$v1 ){

     foreach($b as $k2=>$v2){

         if($k1==$k2){

            $res1[$k1]=$v2-$v1;

         }else{

            $res2[$k2]=v2;

            $res3[$k1]=v1;

         }

     }

}


$res[]=array_merge($res1,$res2,$res3);

echo "<pre>"; print_r($res); echo "</pre>";

但它没有给出我想要的正确结果。


慕少森
浏览 318回答 3
3回答

一只名叫tom的猫

正如评论中提到的,array_walk本质上仍然是一个循环,因为它迭代每个元素。我已经记录了每一步的代码,但基本思想是创建一个合并的数组,然后更改重复项的值。有更有效的方法来解决这个问题,但这说明了一种基本方法。我还应该指出,因为这是使用array_walk匿名函数(闭包),我们必须传入它需要的任何变量(... use (...))。如果你使用一个foreach循环(或for),你不会需要做到这一点,你可以访问的$output,$first和$second直接。https://3v4l.org/WD0t4#v7125<?php$first = [&nbsp; &nbsp; 1 => 4.00,&nbsp; &nbsp; 2 => 3.00,&nbsp; &nbsp; 3 => 8.00,&nbsp; &nbsp; 4 => 4.88,&nbsp; &nbsp; 5 => 7.88,&nbsp; &nbsp; 10 => 17.88];$second = [&nbsp; &nbsp; 1 => 2.00,&nbsp; &nbsp; 3 => 4.00,&nbsp; &nbsp; 4 => 2.88,&nbsp; &nbsp; 7 => 5.0,&nbsp; &nbsp; 8 => 6.00];// Merge the 2 original arrays and preserve the keys// https://stackoverflow.com/q/17462354/296555// The duplicate items' value will be clobbered at this point but we don't care. We'll set them in the `array_walk` function.$output = $first + $second;// Create an array of the duplicates. We'll use these keys to calculate the difference.$both = array_intersect_key($first, $second);// Foreach element in the duplicates, calculate the difference.// Notice that we're passing in `&$output` by reference so that we are modifying the underlying object and not just a copy of it.array_walk($both, function($value, $key) use (&$output, $first, $second) {&nbsp; &nbsp; $output[$key] = $first[$key] - $second[$key];});// Finally, sort the final array by its keys.ksort($output);var_dump($output);// Output//array (size=8)//&nbsp; 1 => float 2//&nbsp; 2 => float 3//&nbsp; 3 => float 4//&nbsp; 4 => float 2//&nbsp; 5 => float 7.88//&nbsp; 7 => float 5//&nbsp; 8 => float 6//&nbsp; 10 => float 17.88以及使用foreach循环的强制性精简版本。<?php$first = [&nbsp; &nbsp; 1 => 4.00,&nbsp; &nbsp; 2 => 3.00,&nbsp; &nbsp; 3 => 8.00,&nbsp; &nbsp; 4 => 4.88,&nbsp; &nbsp; 5 => 7.88,&nbsp; &nbsp; 10 => 17.88];$second = [&nbsp; &nbsp; 1 => 2.00,&nbsp; &nbsp; 3 => 4.00,&nbsp; &nbsp; 4 => 2.88,&nbsp; &nbsp; 7 => 5.0,&nbsp; &nbsp; 8 => 6.00];$output = $first + $second;foreach (array_keys(array_intersect_key($first, $second)) as $key) {&nbsp; &nbsp; $output[$key] = $first[$key] - $second[$key];}ksort($output);var_dump($output);// Output//array (size=8)//&nbsp; 1 => float 2//&nbsp; 2 => float 3//&nbsp; 3 => float 4//&nbsp; 4 => float 2//&nbsp; 5 => float 7.88//&nbsp; 7 => float 5//&nbsp; 8 => float 6//&nbsp; 10 => float 17.88

ABOUTYOU

你非常接近!$count = count($a) + count($b);&nbsp; // figure out how many times we need to loop - count total # of elementsfor ( $key = 1; $key <= $count ; $key++ ){&nbsp; &nbsp; if ( isset($a[$key]) && isset($b[$key]) )&nbsp; $result[$key] = number_format(abs($a[$key] - $b[$key]), 2);&nbsp; &nbsp; elseif ( isset($a[$key]) ) $result[$key] = number_format($a[$key], 2);&nbsp; &nbsp; elseif ( isset($b[$key]) ) $result[$key] = number_format($b[$key], 2);}"number_format" 和 "abs" 函数只是为了使输出看起来与您所说的完全一样。

侃侃无极

请尝试使用以下代码。希望它会给你想要的输出。$res1=$res2=$res3=array();&nbsp; &nbsp; foreach( $a as $k1=>$v1 ){&nbsp; &nbsp; &nbsp; &nbsp; if(isset($b[$k1])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res1[$k1]=$a[$k1]-$b[$k1];&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res2[$k1]=$v1;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; $res3=array_diff_key($b, $a);&nbsp; &nbsp; $result = array_replace_recursive($res1,$res2,$res3);&nbsp; &nbsp; echo "<pre>"; print_r($result); echo "</pre>";
打开App,查看更多内容
随时随地看视频慕课网APP