Home > Backend Development > PHP Tutorial > 请问数组合并有关问题

请问数组合并有关问题

WBOY
Release: 2016-06-13 11:49:05
Original
816 people have browsed it

请教数组合并问题

本帖最后由 lumengabc 于 2014-01-19 20:13:57 编辑
$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 />			);
Copy after login
Copy after login

即还是按数组$a的顺序排序,将$b的值叠加到对应$a
------解决方案--------------------
$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);
Copy after login

Array
(
[sh] => Array
(
[0] => 500
[1] => 0
)

[bj] => Array
(
[0] => 100
[1] => 5
)

[jx] => Array
(
[0] => 20
[1] => 0
)

[cq] => Array
(
[0] => 0
[1] => 50
)

)
------解决方案--------------------
<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 />
Copy after login





引用:
$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 />			);
Copy after login
Copy after login

即还是按数组$a的顺序排序,将$b的值叠加到对应$a


高性能时版主会代码会出现BUG我不知道你的数据量有多大
可以分别运行测试我本地是php5.5.6所有版主代码稍微改动了下否则程序跑不起来(关闭错误输出也不行php版本问题)
<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 />
Copy after login


1w条数据时
版主代码


我的代码



10w条数据时
版主代码


我的代码


100w数据库时
都会出现
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)错误
memory_limit可解决此问题

我本地memory_limit = 128M

版主代码 9.4w 
我的代码 22w

只研究代码不研究其他
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template