Rumah > Soal Jawab > teks badan
Suatu tatasusunan dua dimensi senarai peserta
$arr1=array(
array('name'=>'Zhang San','phone'=>'1354459845','address' => ;'No. 5, First Street'),
array('name'=>'李思' ,'phone'=>'1323439845','address'=>'No 3, First Street'),
array('name'=>'Chen Wu','phone'=>' 1354353145' ,'address'=>'No 2, First Street'),
);
Suatu susunan dua dimensi senarai pemenang loteri bersejarah
$arr2=array(
array('name'=>'mary','phone'=>'1313598445 ','address'=>'No. 8, First Street'),
array('name'=>'jhon','phone'=>'1323298425','addre ss'=>'No. 9, First Street'),
array('name'=>'Chen Wu', 'phone'=>'1353dsad45', 'address'=>'一Jalan No. 10'),
);
Sekarang kita perlu membandingkan dua tatasusunan ini selagi salah satu nama, telefon atau alamat muncul dalam senarai sejarah, kita mesti mengalih keluar dia daripada arr1 yang mengambil bahagian. senarai aktiviti.
Selain dua perbandingan foreach, adakah terdapat cara yang baik untuk mendapatkan hasil senarai aktiviti yang ditapis dengan cepat? Kedua-dua senarai sekurang-kurangnya beberapa ribu Jika anda menggunakan foreach dua kali, kelajuannya akan menjadi sangat perlahan
漂亮男人2017-05-16 13:10:04
$arr = [
["name" => "李四", "phone" => '5555', "address" => 'aaa' ],
["name" => "张三", "phone" => '6666', "address" => 'bbb' ],
["name" => "老王", "phone" => '7777', "address" => 'ccc' ],
];
$filter = [
["name" => "李四", "phone" => '3333', "address" => 'xyz' ],
["name" => "王三", "phone" => '6666', "address" => 'xyz' ],
];
$arr = array_filter($arr, function($i) use ($filter) {
return !in_array($i['name'], array_column($filter, 'name')) && !in_array($i['phone'], array_column($filter, 'phone'));
});
var_dump($arr);
怪我咯2017-05-16 13:10:04
$arr1 = [];
$arr2 = [];
///////准备测试数据
for ($i = 0; $i < 100000; ++$i) {
$arr1_sub = [];
$arr1_sub ['name'] = sprintf('测试%s', rand(0, 10000));
$arr1_sub ['phone'] = sprintf('13%s', rand(111111111, 999999999));
$arr1_sub ['address'] = sprintf('一街%s号', rand(0, 10000));
$arr1[] = $arr1_sub;
$arr2_sub = [];
$arr2_sub['name'] = sprintf('测试%s', rand(0, 10000));
$arr2_sub['phone'] = sprintf('13%s', rand(111111111, 999999999));
$arr2_sub['address'] = sprintf('一街%s号', rand(0, 10000));
$arr2[] = $arr2_sub;
}
//////测试开始
$begin = microtime(true);
$diff_key_array = [
'name' => 'phone',
'phone' => 'phone',
'address' => 'phone',
];
$diff_res_array = [];
///1、根据 key,缓存对一个数据差
foreach ($diff_key_array as $k => $v) {
///找到对应的 name、address、phone 需要留下的数据
$diff_res_array[$k] = array_diff_assoc(array_column($arr2, $v, $k), array_column($arr1, $v, $k));
}
unset($arr1); //清理不需要的数组 arr2
///过滤对应 arr2 数据
$result = array_filter($arr2, function ($value) use ($diff_key_array, &$diff_res_array) {
foreach ($diff_key_array as $k => $v) {
$item = $value[$k];
//利用 php 中,hash 找 key 的方案,实现解决 O(1)查找
if (!isset($diff_res_array[$k][$item])) {
return false;
}
}
return true;
});
unset($diff_key_array);
unset($diff_res_array);
$exec_time = microtime(true) - $begin;
var_dump($exec_time);
var_dump($result);
/// 10万数据,用时约 float 0.69 s
///100万数据,用时约 float 14.86 s