时间:2021-07-01 10:21:17 帮助过:15人阅读
$a = array('sh'=>500,'bj'=>100, 'jx'=>20);
$b = array('bj'=>5, 'cq'=>50, 'sh'=>0);
//要求合并$a和$b, 得到结果:
$c =array(
'sh'=>array(500,0),
'bj'=>array(100,0),
'jx'=>array(20,0),
'cq'=>array(0,50),
);
$a = array('sh'=>500,'bj'=>100, 'jx'=>20);
$b = array('bj'=>5, 'cq'=>50, 'sh'=>0);
$keys = array_keys(array_merge($a,$b));
foreach($keys as $k){
$ar[$k]=array($a[$k] ? $a[$k] : 0 , $b[$k] ? $b[$k] : 0);
}
print_r($ar);
$a = array('sh' => 500, 'bj' => 100, 'jx' => 20);
$b = array('bj' => 5, 'cq' => 50, 'sh' => 0);
var_dump(array_merge_recursive($a+array_fill_keys(array_keys(array_merge($a, $b)), '0'), $b));
$pagestartime = microtime();
$b = $a = range(0, 100000);
array_merge_recursive($a + array_fill_keys(array_keys(array_merge($a, $b)), '0'), $b);
$pageendtime = microtime();
$starttime = explode(" ", $pagestartime);
$endtime = explode(" ", $pageendtime);
$totaltime = $endtime[0] - $starttime[0] + $endtime[1] - $starttime[1];
$timecost = sprintf("%s", $totaltime);
var_dump($timecost);
$pagestartime = microtime();
$b = $a = range(0, 100000);
$keys = array_keys(array_merge($a, $b));
$ar=array();
foreach ($keys as $k) {
$ar[$k] = array(isset($a[$k]) ? $a[$k] : 0, isset($a[$k]) ? $b[$k] : 0);
}
$pageendtime = microtime();
$starttime = explode(" ", $pagestartime);
$endtime = explode(" ", $pageendtime);
$totaltime = $endtime[0] - $starttime[0] + $endtime[1] - $starttime[1];
$timecost = sprintf("%s", $totaltime);
var_dump($timecost);