我想在php中比较两个数组。我的数组看起来像这样
Array (
[0] => Array ( [Id] => 1 [row1] => 1458)
[1] => Array ( [Id] => 2 [row1] => 16)
[2] => Array ( [Id] => 3 [row1] => 115)
[3] => Array ( [Id] => 4 [row1] => 18)
[4] => Array ( [Id] => 5 [row1] => 13)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)
Array (
[0] => Array ( [Id] => 1 [row1] => 158)
[1] => Array ( [Id] => 2 [row1] => 165)
[2] => Array ( [Id] => 3 [row1] => 111)
[3] => Array ( [Id] => 4 [row1] => 186)
[4] => Array ( [Id] => 5 [row1] => 3)
)
首先,array1大小和array2大小始终不相等。 array1中的Id值可能会或可能不会出现在array2中,如果该值不存在,则函数必须在array3中打印总索引,如
[someindex] => Array ( [Id] => 6 [row1] => 13 )
如果存在,函数应该将array1的row1减去array2的row1并在array3中打印,就像这样
[someindex] => Array ( [Id] => 1 [row1] => 1300)
我的最终输出应该是,
Array (
[0] => Array ( [Id] => 1 [row1] => 1300)
[1] => Array ( [Id] => 2 [row1] => -149)
[2] => Array ( [Id] => 3 [row1] => 4)
[3] => Array ( [Id] => 4 [row1] => -168)
[4] => Array ( [Id] => 5 [row1] => 10)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)
任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
$arr1 = Array (
0 => Array ('Id' => 1, 'row1' => 1458)
,1 => Array ('Id' => 2, 'row1' => 16)
,2 => Array ('Id' => 3, 'row1' => 115)
,3 => Array ('Id' => 4, 'row1' => 18)
,4 => Array ('Id' => 5, 'row1' => 13)
,5 => Array ('Id' => 6, 'row1' => 13)
,6 => Array ('Id' => 7, 'row1' => 131)
);
$arr2 = Array (
0 => Array('Id' => 1, 'row1' => 158)
,1 => Array('Id' => 2, 'row1' => 165)
,2 => Array('Id' => 3, 'row1'=> 111)
,3 => Array('Id' => 4, 'row1' => 186)
,4 => Array('Id' => 5, 'row1' => 3)
);
$final = array();
foreach($arr1 as $k => $sec)
{
$sub = 0;
foreach($arr2 as $sec2)
{
if($sec2['Id']==$sec['Id'])
{
$sub = $sec2['row1'];
break;
}
}
$sec['row1'] -= $sub;
//Or to save to another element:
//$sec['row2'] = $sec['row1']-$sub;
$final[] = $sec;
}
echo '<pre>'.print_r($final,true).'</pre>';
输出:
Array (
[0] => Array ( [Id] => 1 [row1] => 1300)
[1] => Array ( [Id] => 2 [row1] => -149)
[2] => Array ( [Id] => 3 [row1] => 4)
[3] => Array ( [Id] => 4 [row1] => -168)
[4] => Array ( [Id] => 5 [row1] => 10)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)
答案 1 :(得分:0)
不知道这究竟是您想要的,但您可以检查array_key_exists是否存在密钥:
$array3 = array();
foreach ($array1 as $k => $v) {
if (array_key_exists($k, $array2)) {
$array3[] = $v - $array2[$k];
} else {
$array3[] = $v;
}
}
答案 2 :(得分:0)
首先,您必须使用Id
值作为键来搜索第二个数组,或者编写搜索功能。我选择前者:
$searchable = array_reduce($array2, function(&$result, $item) {
return $result + array($item['Id'] => $item['row1']);
}, array());
array_reduce()
函数以空数组开始,并使用数组加法运算符构建它;这将创建一个可以使用id解除引用的数组。
然后在第一个数组上执行映射操作:
$array3 = array_map(function($item) use ($searchable) {
$value = isset($searchable[$item['Id']]) ? $searchable[$item['Id']] : 0;
$item['row1'] -= $value;
// $item['row2'] = $item['row1'] - $value;
return $item;
}, $array1);
执行地图操作会保留原始数组,并使用您在回调函数中选择的值创建一个新数组。
答案 3 :(得分:0)
这会创建一个索引数组($new
),以防主键与原始数组中的主键不匹配。
$arr = array (
0 => array ( 'Id' => '1','row1' => 1458),
1 => array ( 'Id' => '2','row1' => 16),
2 => array ( 'Id' => '3','row1' => 115),
3 => array ( 'Id' => '4','row1' => 18),
4 => array ( 'Id' => '5','row1' => 13),
5 => array ( 'Id' => '6','row1' => 13),
6 => array ( 'Id' => '7','row1' => 131));
$arr2 = array (
0 => array ( 'Id' => '1','row1' => 158),
1 => array ( 'Id' => '2','row1' => 165),
2 => array ( 'Id' => '3','row1' => 111),
3 => array ( 'Id' => '4','row1' => 186),
4 => array ( 'Id' => '5','row1' => 3));
$new = array();
foreach ($arr2 as $key => $value){
$new[$value['Id']] = $value['row1'];
}
foreach ($arr as $key => $value){
if (isset($new[$value['Id']])){
$arr[$key]['row1'] -= $new[$value['Id']];
}
}
print_r($arr);