Php array merging-php Tutorial

Source: Internet
Author: User
Php array merging problems two-dimensional arrays need to be merged
$arr2=array(    "hdzt_show" =>       array (                        "local_date" => "1420128000",            "parent_channel" => "hdzt_show",            "channel" => "hdzt_show",            "speak_uv" => "39",            "speak_amount" => "67"                    ));$arr=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" => "15"        ));
In this case, the local_date and parent_channel in front of array_merge_recursive will repeatedly generate an array.

Want the following results
$arr=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" => "15""speak_uv" => "39",            "speak_amount" => "67"        ));
Hdzt_show and uv are not fixed.


Reply to discussion (solution)

This array has thousands of methods, so try to be faster.

If the two arrays have the same size and order

$arr2=array(    "hdzt_show" =>       array (                         "local_date" => "1420128000",            "parent_channel" => "hdzt_show",            "channel" => "hdzt_show",            "speak_uv" => "39",            "speak_amount" => "67"                     ));$arr=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" => "15"        ));$r = array_combine(array_keys($arr), array_map('array_merge', $arr, $arr2));var_export($r);

If the two arrays may have different scales (the number of the first dimension is different and the order is different ),
foreach($arr as $k=>$v) {  if(isset($arr2[$k])) $r[$k] = array_merge($v, $arr2[$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' => '0',    'pv' => '15',    'speak_uv' => '39',    'speak_amount' => '67',  ),)

What do you do if it already exists? Assume that the field repeats the last one and the arrays to be processed are two-dimensional arrays with the same key.


function merge_arr($arr,$key1=null){if(!$key1) $key1= key($arr[0]);$return_arr = array();foreach($arr as $key=>$val){foreach($val[$key1] as $k=>$v){$return_arr[$key1][$k] = $v;}}return $return_arr;}$arr2=array(    "hdzt_show" =>       array (                         "local_date" => "1420128000",            "parent_channel" => "hdzt_show",            "channel" => "hdzt_show",            "speak_uv" => "39",            "speak_amount" => "67"                     ));$arr=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" => "15"        ));print_r(merge_arr(array($arr2,$arr)));exit;


The result is as follows:

Array(    [hdzt_show] => Array        (            [local_date] => 1420128000            [parent_channel] => hdzt_show            [channel] => hdzt_show            [speak_uv] => 39            [speak_amount] => 67            [uv] => 7            [fuv] => 6            [valid_user] => 2            [duration] => 586            [register] => 0            [third_register] => 0            [pv] => 15        ))

If the two arrays have the same size and order

$arr2=array(    "hdzt_show" =>       array (                         "local_date" => "1420128000",            "parent_channel" => "hdzt_show",            "channel" => "hdzt_show",            "speak_uv" => "39",            "speak_amount" => "67"                     ));$arr=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" => "15"        ));$r = array_combine(array_keys($arr), array_map('array_merge', $arr, $arr2));var_export($r);

If the two arrays may have different scales (the number of the first dimension is different and the order is different ),
foreach($arr as $k=>$v) {  if(isset($arr2[$k])) $r[$k] = array_merge($v, $arr2[$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' => '0',    'pv' => '15',    'speak_uv' => '39',    'speak_amount' => '67',  ),)



How can I change the number of combinations?


If the two arrays have the same size and order

$arr2=array(    "hdzt_show" =>       array (                         "local_date" => "1420128000",            "parent_channel" => "hdzt_show",            "channel" => "hdzt_show",            "speak_uv" => "39",            "speak_amount" => "67"                     ));$arr=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" => "15"        ));$r = array_combine(array_keys($arr), array_map('array_merge', $arr, $arr2));var_export($r);

If the two arrays may have different scales (the number of the first dimension is different and the order is different ),
foreach($arr as $k=>$v) {  if(isset($arr2[$k])) $r[$k] = array_merge($v, $arr2[$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' => '0',    'pv' => '15',    'speak_uv' => '39',    'speak_amount' => '67',  ),)



How can I change the number of combinations?



Refer to my method to package all the arrays and pass them in.



If the two arrays have the same size and order

$arr2=array(    "hdzt_show" =>       array (                         "local_date" => "1420128000",            "parent_channel" => "hdzt_show",            "channel" => "hdzt_show",            "speak_uv" => "39",            "speak_amount" => "67"                     ));$arr=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" => "15"        ));$r = array_combine(array_keys($arr), array_map('array_merge', $arr, $arr2));var_export($r);

If the two arrays may have different scales (the number of the first dimension is different and the order is different ),
foreach($arr as $k=>$v) {  if(isset($arr2[$k])) $r[$k] = array_merge($v, $arr2[$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' => '0',    'pv' => '15',    'speak_uv' => '39',    'speak_amount' => '67',  ),)



How can I change the number of combinations?



Refer to my method to package all the arrays and pass them in.

... When you say this, I don't understand it. please post it to me. you have two foreach. There are thousands of arrays that need to be merged. Will it be slow?

You can use the xu method to upload all arrays.

The efficiency of my foreach is not very slow, because the principle is the same.

The first layer of foreach is. you can process more than 1000 arrays in turn. The first layer of foreach is the basic idea for processing each array. the new key is pushed into the result array, and 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 method to upload all arrays.

The efficiency of my foreach is not very slow, because the principle is the same.

The first layer of foreach is. you can process more than 1000 arrays in turn. The first layer of foreach is the basic idea for processing each array. the new key is pushed into the result array, and the old key replaces the corresponding value. the final array has all the keys and each key has only one value.


Okay. can you paste the three arrays? I'm a newbie, but I don't quite understand this method.

What is going on with multiple arrays (take it for granted ?)
If the array before sang needs to be merged, you have to consider whether there is a problem.
If post-event merging is required, use array_merge_recursive.
Then process the result.

$arr2=array(    "hdzt_show" =>       array (                          "local_date" => "1420128000",            "parent_channel" => "hdzt_show",            "channel" => "hdzt_show",            "speak_uv" => "39",            "speak_amount" => "67"                      ));$arr=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" => "15"        ));$r = array_merge_recursive($arr, $arr2);foreach($r as &$t)  foreach($t as &$v) if(is_array($v)) $v = current($v);print_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] => 15            [speak_uv] => 39            [speak_amount] => 67        ))

What is going on with multiple arrays (take it for granted ?)
If the array before sang needs to be merged, you have to consider whether there is a problem.
If post-event merging is required, use array_merge_recursive.
Then process the result.

$arr2=array(    "hdzt_show" =>       array (                          "local_date" => "1420128000",            "parent_channel" => "hdzt_show",            "channel" => "hdzt_show",            "speak_uv" => "39",            "speak_amount" => "67"                      ));$arr=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" => "15"        ));$r = array_merge_recursive($arr, $arr2);foreach($r as &$t)  foreach($t as &$v) if(is_array($v)) $v = current($v);print_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] => 15            [speak_uv] => 39            [speak_amount] => 67        ))

Thank you!

Thank you!

Thank you for the hard work of the two Daniel upstairs.

Merge_arr (array ($ arr2, $ arr, $ arr3, $ arr4, $ arr5,..., $ arr1000 ));

You can help.

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.