两个数组循环对比方法

$new_res = db()->query($sql);
$old_res = db()->query($sql);
result = array();
foreach($old_res as $old_v){
$result[$old_v['account_number']] = $old_v;
}
$i = 0;
foreach($new_res as $key => $value){
if($result[$value['account_number']]['year'] == $value['year'] &&
$result[$value['account_number']]['month'] == $value['month'] &&
$result[$value['account_number']]['already_num'] == $value['already_num'] &&
$result[$value['account_number']]['status'] == $value['status']
){


}

上面的适应key唯一的情况
$result[$old_v['account_number']] = $old_v;

如果key不唯一,需要用
$result[$old_v['account_number']][] = $old_v;

见下面例子:

$books = [
[
'name'=>'a',
'type'=>1,
'price'=>100
],
[
'name'=>'b',
'type'=>2,
'price'=>200
],
[
'name'=>'c',
'type'=>1,
'price'=>150
]
];

$result = [];
foreach($books as $key=>$value){
$result[$value['type']][] = $value;
}




posted on 2019-11-22 16:05  日月星辰$  阅读(1312)  评论(0编辑  收藏  举报