Problems with merging PHP arrays

Source: Internet
Author: User
Two-dimensional arrays need to be merged
$arr 2=array (    "hdzt_show" = =       Array (                        "local_date" = "1420128000",            "Parent_channel" and "Hdzt" _show ",            " channel "=" Hdzt_show ",            " Speak_uv "and" =array ",            " Speak_amount "and" ("                    )"); $arr    "hdzt_show" = =        Array (            "local_date" = "1420128000",            "Parent_channel" and "Hdzt_show",            "Channel" = "Hdzt_show",            "UV" = "7",            "FUV" and "6",            "Valid_user" and "2",            " duration "=" and "586",            "register" = "0",            "Third_register" and "0",            "PV" and "        (") ");
In this case, the Local_date,parent_channel in front of array_merge_recursive will generate the array repeatedly.

Want the following results
$arr =array (    "hdzt_show" = =        Array (            "local_date" = "1420128000",            "Parent_channel" and "Hdzt_" Show "            channel" = "Hdzt_show",            "UV" = "7",            "FUV" and "6",            "Valid_user" and "2",            " duration "=" and "586",            "register" and "0",            "Third_register" and "0",            "PV" and "All" "speak_uv" and "=" ("            speak_amount"        );
Hdzt_show This key and UV these key number names are not fixed


Reply to discussion (solution)

This array has thousands of ways to make it as fast as possible.

If the two arrays are the same size, the order is the same

$arr 2=array (    "hdzt_show" = =       Array (                         "local_date" = "1420128000",            "Parent_channel" and "Hdzt" _show ",            " channel "=" Hdzt_show ",            " Speak_uv "and" =array ",            " Speak_amount "and" ("                     )"); $arr    "hdzt_show" = =        Array (            "local_date" = "1420128000",            "Parent_channel" and "Hdzt_show",            "Channel" = "Hdzt_show",            "UV" = "7",            "FUV" and "6",            "Valid_user" and "2",            " duration "=" and "586",            "register" = "0",            "Third_register" and "0", "PV" and "(") ")        ; $r = Array_combine (Array_keys ($arr), Array_map (' Array_merge ', $arr, $arr 2)); Var_export ($R);

Consider the possible scale differences in the two array (the number of first dimensions is different and the order is different)
foreach ($arr as $k = + $v) {  if (isset ($arr 2[$k)) $r [$k] = Array_merge ($v, $arr 2[$k]);} Var_export ($R);
Array ('  hdzt_show ' = =   Array (    ' local_date ' = ' 1420128000 ',    ' parent_channel ' = ' hdzt_show ') ,    ' channel ' = ' hdzt_show ',    ' uv ' = ' 7 ',    ' fuv ' = ' 6 ',    ' valid_user ' = ' 2 ',    ' duration ' = ' 586 ',    ' register ' = ' 0 ',    ' third_register ' and ' 0 ',    ' pv ' = ' + ',    ' speak_uv ' = ' (Speak_amount ', ' + '),)  

If you repeat, what do you do with it? Suppose that the field repeats the last one, and that the pending array is a two-dimensional array of the same key so to write


function Merge_arr ($arr, $key 1=null) {if (! $key 1) $key 1= key ($arr [0]), $return _arr = Array (); foreach ($arr as $key =>$ val) {foreach ($val [$key 1] as $k = = $v) {$return _arr[$key 1][$k] = $v;}} return $return _arr;} $arr 2=array (    "hdzt_show" = =       Array (                         "local_date" = "1420128000",            "Parent_channel" and "Hdzt" _show ",            " channel "=" Hdzt_show ",            " Speak_uv "and" =array ",            " Speak_amount "and" ("                     )"); $arr    "hdzt_show" = =        Array (            "local_date" = "1420128000",            "Parent_channel" and "Hdzt_show",            "Channel" = "Hdzt_show",            "UV" = "7",            "FUV" and "6",            "Valid_user" and "2",            " duration "=" and "586",            "register" = "0",            "Third_register" and "0",            "PV" and "(") ")        ;p Rint_ R (Merge_arr (Array ($arr 2, $arr)); exit;


The results are as follows

Array (    [hdzt_show] = = Array        (            [local_date] = 1420128000            [Parent_channel] = Hdzt_show            [Channel] = Hdzt_show            [SPEAK_UV] = [            speak_amount] = [            UV] = 7            [FUV] + 6            [ Valid_user] = 2            [duration] = 586            [Register] + 0            [Third_register] + 0            [PV] = 15< c14/>))

If the two arrays are the same size, the order is the same

$arr 2=array (    "hdzt_show" = =       Array (                         "local_date" = "1420128000",            "Parent_channel" and "Hdzt" _show ",            " channel "=" Hdzt_show ",            " Speak_uv "and" =array ",            " Speak_amount "and" ("                     )"); $arr    "hdzt_show" = =        Array (            "local_date" = "1420128000",            "Parent_channel" and "Hdzt_show",            "Channel" = "Hdzt_show",            "UV" = "7",            "FUV" and "6",            "Valid_user" and "2",            " duration "=" and "586",            "register" = "0",            "Third_register" and "0", "PV" and "(") ")        ; $r = Array_combine (Array_keys ($arr), Array_map (' Array_merge ', $arr, $arr 2)); Var_export ($R);

Consider the possible scale differences in the two array (the number of first dimensions is different and the order is different)
foreach ($arr as $k = + $v) {  if (isset ($arr 2[$k)) $r [$k] = Array_merge ($v, $arr 2[$k]);} Var_export ($R);
Array ('  hdzt_show ' = =   Array (    ' local_date ' = ' 1420128000 ',    ' parent_channel ' = ' hdzt_show ') ,    ' channel ' = ' hdzt_show ',    ' uv ' = ' 7 ',    ' fuv ' = ' 6 ',    ' valid_user ' = ' 2 ',    ' duration ' = ' 586 ',    ' register ' = ' 0 ',    ' third_register ' and ' 0 ',    ' pv ' = ' + ',    ' speak_uv ' = ' (Speak_amount ', ' + '),)  



How do you change this if you can replace it with multiple array merges?


If the two arrays are the same size, the order is the same

$arr 2=array (    "hdzt_show" = =       Array (                         "local_date" = "1420128000",            "Parent_channel" and "Hdzt" _show ",            " channel "=" Hdzt_show ",            " Speak_uv "and" =array ",            " Speak_amount "and" ("                     )"); $arr    "hdzt_show" = =        Array (            "local_date" = "1420128000",            "Parent_channel" and "Hdzt_show",            "Channel" = "Hdzt_show",            "UV" = "7",            "FUV" and "6",            "Valid_user" and "2",            " duration "=" and "586",            "register" = "0",            "Third_register" and "0", "PV" and "(") ")        ; $r = Array_combine (Array_keys ($arr), Array_map (' Array_merge ', $arr, $arr 2)); Var_export ($R);

Consider the possible scale differences in the two array (the number of first dimensions is different and the order is different)
foreach ($arr as $k = + $v) {  if (isset ($arr 2[$k)) $r [$k] = Array_merge ($v, $arr 2[$k]);} Var_export ($R);
Array ('  hdzt_show ' = =   Array (    ' local_date ' = ' 1420128000 ',    ' parent_channel ' = ' hdzt_show ') ,    ' channel ' = ' hdzt_show ',    ' uv ' = ' 7 ',    ' fuv ' = ' 6 ',    ' valid_user ' = ' 2 ',    ' duration ' = ' 586 ',    ' register ' = ' 0 ',    ' third_register ' and ' 0 ',    ' pv ' = ' + ',    ' speak_uv ' = ' (Speak_amount ', ' + '),)  



How do you change this if you can replace it with multiple array merges?



Refer to my method to package all the arrays in



If the two arrays are the same size, the order is the same

$arr 2=array (    "hdzt_show" = =       Array (                         "local_date" = "1420128000",            "Parent_channel" and "Hdzt" _show ",            " channel "=" Hdzt_show ",            " Speak_uv "and" =array ",            " Speak_amount "and" ("                     )"); $arr    "hdzt_show" = =        Array (            "local_date" = "1420128000",            "Parent_channel" and "Hdzt_show",            "Channel" = "Hdzt_show",            "UV" = "7",            "FUV" and "6",            "Valid_user" and "2",            " duration "=" and "586",            "register" = "0",            "Third_register" and "0", "PV" and "(") ")        ; $r = Array_combine (Array_keys ($arr), Array_map (' Array_merge ', $arr, $arr 2)); Var_export ($R);

Consider the possible scale differences in the two array (the number of first dimensions is different and the order is different)
foreach ($arr as $k = + $v) {  if (isset ($arr 2[$k)) $r [$k] = Array_merge ($v, $arr 2[$k]);} Var_export ($R);
Array ('  hdzt_show ' = =   Array (    ' local_date ' = ' 1420128000 ',    ' parent_channel ' = ' hdzt_show ') ,    ' channel ' = ' hdzt_show ',    ' uv ' = ' 7 ',    ' fuv ' = ' 6 ',    ' valid_user ' = ' 2 ',    ' duration ' = ' 586 ',    ' register ' = ' 0 ',    ' third_register ' and ' 0 ',    ' pv ' = ' + ',    ' speak_uv ' = ' (Speak_amount ', ' + '),)  



How do you change this if you can replace it with multiple array merges?



Refer to my method to package all the arrays in

。。。 When you say it, I don't know how much trouble you have to put it on me. There are two of them. There are thousands of arrays that need to be merged so will it be slow?

You can use the Xu big way to send the array in.

My two foreach efficiency will not be very slow because the principle is the same

The first layer of foreach, you know, you're going to do this. 1000 + arrays in turn, processing inside a layer of foreach is the basic idea of each array specific processing is that each new key is pressed into the result array the old key replaces the corresponding value the final array has all the keys and each key has only one value

You can use the Xu big way to send the array in.

My two foreach efficiency will not be very slow because the principle is the same

The first layer of foreach, you know, you're going to do this. 1000 + arrays in turn, processing inside a layer of foreach is the basic idea of each array specific processing is that each new key is pressed into the result array the old key replaces the corresponding value the final array has all the keys and each key has only one value


Okay, you can put it on. Three arrays how do I write? I'm a novice.

What's going on with multiple arrays (take it for granted?) )
If there is really a need to merge the array before the Mulberry, you have to think about the idea of whether there is a problem.
If you insist on merging afterwards, then you should use Array_merge_recursive.
And then deal with the resulting results.

 $arr 2=array ("hdzt_show" = = Array ("Local_date" => ;  "1420128000", "parent_channel" = "Hdzt_show", "channel" = "Hdzt_show", "Speak_uv"            = "" "," Speak_amount "and" (")"); $arr =array ("hdzt_show" = Array ( "Local_date" = "1420128000", "parent_channel" = "Hdzt_show", "channel" and "Hdzt" _show "," UV "=" 7 "," FUV "=" 6 "," Valid_user "and" 2 "," duration "=& Gt "586", "register" = "0", "third_register" = "0", "PV" and "(") "); $r = AR Ray_merge_recursive ($arr, $arr 2), foreach ($r as & $t) foreach ($t as & $v) if (Is_array ($v)) $v = current ($v);p Rint_r ($r); 
Array (    [hdzt_show] = = Array        (            [local_date] = 1420128000            [Parent_channel] = Hdzt_show            [Channel] = Hdzt_show            [UV] = 7            [FUV] = 6            [Valid_user] = 2            [duration] = 586            [ Register] = 0            [Third_register] + 0            [PV] =            [SPEAK_UV] [[            speak_amount] = 67        ))

What's going on with multiple arrays (take it for granted?) )
If there is really a need to merge the array before the Mulberry, you have to think about the idea of whether there is a problem.
If you insist on merging afterwards, then you should use Array_merge_recursive.
And then deal with the resulting results.

 $arr 2=array ("hdzt_show" = = Array ("Local_date" => ;  "1420128000", "parent_channel" = "Hdzt_show", "channel" = "Hdzt_show", "Speak_uv"            = "" "," Speak_amount "and" (")"); $arr =array ("hdzt_show" = Array ( "Local_date" = "1420128000", "parent_channel" = "Hdzt_show", "channel" and "Hdzt" _show "," UV "=" 7 "," FUV "=" 6 "," Valid_user "and" 2 "," duration "=& Gt "586", "register" = "0", "third_register" = "0", "PV" and "(") "); $r = AR Ray_merge_recursive ($arr, $arr 2), foreach ($r as & $t) foreach ($t as & $v) if (Is_array ($v)) $v = current ($v);p Rint_r ($r); 
Array (    [hdzt_show] = = Array        (            [local_date] = 1420128000            [Parent_channel] = Hdzt_show            [Channel] = Hdzt_show            [UV] = 7            [FUV] = 6            [Valid_user] = 2            [duration] = 586            [ Register] = 0            [Third_register] + 0            [PV] =            [SPEAK_UV] [[            speak_amount] = 67        ))

It's finally done, thank you.

Thank you both for your hard work.

Thanks for the two of us upstairs, Daniel is a hard-working man.

Merge_arr (Array ($arr 2, $arr, $arr 3, $arr 4, $arr 5,..., $arr 1000));

I'll help you, you're welcome.

  • 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.