请教数组合并问题
$a = array('sh'=>500,'bj'=>100, 'jx'=>20);<br />$b = array('bj'=>5, 'cq'=>50, 'sh'=>0);<br /><br />//要求合并$a和$b, 得到结果:<br />$c =array(<br /> 'sh'=>array(500,0),<br /> 'bj'=>array(100,0),<br /> 'jx'=>array(20,0),<br /> 'cq'=>array(0,50),<br /> );
$a = array('sh'=>500,'bj'=>100, 'jx'=>20);<br />$b = array('bj'=>5, 'cq'=>50, 'sh'=>0);<br />$keys = array_keys(array_merge($a,$b));<br /><br />foreach($keys as $k){<br /> $ar[$k]=array($a[$k] ? $a[$k] : 0 , $b[$k] ? $b[$k] : 0);<br />}<br />print_r($ar);
<br /><?php<br /><br />$a = array('sh' => 500, 'bj' => 100, 'jx' => 20);<br />$b = array('bj' => 5, 'cq' => 50, 'sh' => 0);<br />var_dump(array_merge_recursive($a+array_fill_keys(array_keys(array_merge($a, $b)), '0'), $b));<br />
<br /><?php<br /><br />$pagestartime = microtime();<br />$b = $a = range(0, 100000);<br />array_merge_recursive($a + array_fill_keys(array_keys(array_merge($a, $b)), '0'), $b);<br />$pageendtime = microtime();<br />$starttime = explode(" ", $pagestartime);<br />$endtime = explode(" ", $pageendtime);<br />$totaltime = $endtime[0] - $starttime[0] + $endtime[1] - $starttime[1];<br />$timecost = sprintf("%s", $totaltime);<br />var_dump($timecost);<br /><br /><br />$pagestartime = microtime();<br />$b = $a = range(0, 100000);<br />$keys = array_keys(array_merge($a, $b));<br />$ar=array();<br />foreach ($keys as $k) {<br /> $ar[$k] = array(isset($a[$k]) ? $a[$k] : 0, isset($a[$k]) ? $b[$k] : 0);<br />}<br />$pageendtime = microtime();<br />$starttime = explode(" ", $pagestartime);<br />$endtime = explode(" ", $pageendtime);<br />$totaltime = $endtime[0] - $starttime[0] + $endtime[1] - $starttime[1];<br />$timecost = sprintf("%s", $totaltime);<br />var_dump($timecost);<br />