Questions about array merging

Source: Internet
Author: User
Ask the array merging question. This post was last edited by lumengabc from 2014-01-1920: 13: 57 $ a & nbsp; array (sh & gt; 500, bj & gt; 100, & nbsp; jx & gt; 20); $ B & nbsp; array (bj & gt; 5, & nbsp; 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.