Ask the array merging question. This post was last edited by lumengabc in 2014-01-1920: 13: 57 $ a & nbsp ;=& nbsp; array ('sh' = & gt; 500, 'BJ '= & gt; 100, & nbsp; 'jx' = ask about array merging
This post was last edited by lumengabc at 20:13:57
$ A = array ('sh' => 500, 'BJ '=> 100, 'jx' => 20 );
$ B = array ('BJ '=> 5, 'CQ' => 50, 'sh' => 0 );
// Merge $ a and $ B. The result is as follows:
$ C = array (
'Sh' => array (, 0 ),
'BJ '=> array ),
'Jx' => array (20, 0 ),
'CQ '=> array (0, 50 ),
);
In the order of array $ a, the $ B value is superimposed to the corresponding $
------ Solution --------------------
$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);
Array
(
[Sh] => Array
(
[0] = & gt; 500
[1] => 0
)
[Bj] => Array
(
[0] = & gt; 100
[1] => 5
)
[Jx] => Array
(
[0] => 20
[1] => 0
)
[Cq] => Array
(
[0] => 0
[1] => 50
)
)
------ Solution --------------------
$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));
Reference:
$ A = array ('sh' => 500, 'BJ '=> 100, 'jx' => 20 );
$ B = array ('BJ '=> 5, 'CQ' => 50, 'sh' => 0 );
// Merge $ a and $ B. The result is as follows:
$ C = array (
'Sh' => array (, 0 ),
'BJ '=> array ),
'Jx' => array (20, 0 ),
'CQ '=> array (0, 50 ),
);
In the order of array $ a, the $ B value is superimposed to the corresponding $
When the performance is high, the moderator may encounter code bugs. I don't know how big your data volume is.
You can run the test separately. my local version is php5.5.6. the main code of all versions has been slightly changed. Otherwise, the program cannot run. (if the error output is disabled, the php version cannot be used)
$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);
Data Records
Moderator code
My code
10 million data records
Moderator code
My code
Million database hours
Will appear
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) error
Memory_limit can solve this problem
My local memory_limit = 128 M
Moderator code 9.4
My code is 22 w
Only study the code. do not study others.