Ask the question about array merging

Source: Internet
Author: User
Ask for an array merge problem
This post was last edited by lumengabc on 2014-01-19 20:13:57

$a = array (' sh ' =>500, ' BJ ' =>100, ' JX ' =>20);
$b = Array (' BJ ' =>5, ' CQ ' =>50, ' sh ' =>0);

Ask to merge $ A and $b to get the result:
$c =array (
' Sh ' =>array (500,0),
' BJ ' =>array (100,0),
' JX ' =>array (20,0),
' CQ ' =>array (0,50),
);

That is, in the order of the array $ A, the value of $b is superimposed to the corresponding $ A
------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] = 500
[1] = 0
)

[BJ] = = Array
(
[0] = 100
[1] = 5
)

[JX] = = Array
(
[0] = 20
[1] = 0
)

[CQ] = Array
(
[0] = 0
[1] = 50
)

)
------Solution--------------------


$a = array (' sh ' = +, ' bj ' = +, ' jx ' = 20);
$b = Array (' bj ' = = 5, ' cq ' = ' + ', ' sh ' = 0);
Var_dump (array_merge_recursive ($a +array_fill_keys (Array_keys (Array_merge ($a, $b)), ' 0 '), $b));





References:
$a = array (' sh ' =>500, ' BJ ' =>100, ' JX ' =>20);
$b = Array (' BJ ' =>5, ' CQ ' =>50, ' sh ' =>0);

Ask to merge $ A and $b to get the result:
$c =array (
' Sh ' =>array (500,0),
' BJ ' =>array (100,0),
' JX ' =>array (20,0),
' CQ ' =>array (0,50),
);

That is, in the order of the array $ A, the value of $b is superimposed to the corresponding $ A


High performance when the moderator code will be a bug I don't know how big your data is.
You can run the test separately I am php5.5.6 all the moderator code changes slightly or the program will not run (turn off the error output is not a problem with PHP version)


$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);


1w Data
Moderator Code


My Code



10w data
Moderator Code


My Code


100w Database
will appear
Fatal error:allowed memory size of 134217728 bytes exhausted (tried to allocate + bytes) error
Memory_limit to resolve this issue

My local memory_limit = 128M

Moderator Code 9.4W
My Code for 22w

Only research code does not study other
  • 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.