0

我想比较 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)
)

任何人都可以帮助我解决这个问题。

4

4 回答 4

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)
)
于 2013-08-12T09:44:51.187 回答
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);

执行映射操作会保留原始数组并使用您在回调函数中选择的值创建一个新数组。

于 2013-08-12T09:54:57.013 回答
0

这将创建一个索引数组 ( $new),以防主键与原始数组中的 of 不匹配。

$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);
于 2013-08-12T09:56:32.553 回答
0

不知道这是否正是您想要的,但您可以使用array_key_exists检查密钥是否存在:

$array3 = array();
foreach ($array1 as $k => $v) {
        if (array_key_exists($k, $array2)) {
            $array3[] = $v - $array2[$k];
        } else {
            $array3[] = $v;
        }
    }
于 2013-08-12T09:37:34.993 回答