首頁  >  文章  >  後端開發  >  php 数组合并的问题

php 数组合并的问题

WBOY
WBOY原創
2016-06-23 13:40:43839瀏覽

二维数组需要合并   

$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"        ));
  用这个的话array_merge_recursive    前面的local_date,parent_channel 会重复 生成数组 

想要下面的结果
$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 这个键  还有uv 这些键数量 名称都是不固定的  


回复讨论(解决方案)

这个数组有上千个  所以尽量快一点的方法

如果两数组规模一样,次序也一样则

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

若考虑到两数组可能存在规模上的差异(第一维数量不同、排列次序不同)则
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',  ),)

如果重复了你怎么处理?  假设字段重复取最后一个,并且待处理数组都是同样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;


结果如下

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

如果两数组规模一样,次序也一样则

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

若考虑到两数组可能存在规模上的差异(第一维数量不同、排列次序不同)则
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',  ),)



这个可以   换成多个数组合并的话要怎么改呢 


如果两数组规模一样,次序也一样则

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

若考虑到两数组可能存在规模上的差异(第一维数量不同、排列次序不同)则
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',  ),)



这个可以   换成多个数组合并的话要怎么改呢 

参考我的方法  把所有数组打包传进去



如果两数组规模一样,次序也一样则

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

若考虑到两数组可能存在规模上的差异(第一维数量不同、排列次序不同)则
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',  ),)



这个可以   换成多个数组合并的话要怎么改呢 

参考我的方法  把所有数组打包传进去   。。。 说时候 你这个我没看太懂  麻烦给我贴一下吧 还有就是 你这个是俩个foreach  一共有上千条数组需要合并  这样的话会不会很慢呢?

你可以用xu大的方法 把数组都传进去就可以了

我这两个foreach效率不会有什么特别慢 因为原理是一样的   

第一层foreach就是吧你这个1000多个数组依次处理   里边一层foreach是每个数组具体的处理    基本思路就是每一个新的key就压入结果数组 老的key就替换对应值 最终的数组有所有的key  并且每个key只有一个值

你可以用xu大的方法 把数组都传进去就可以了

我这两个foreach效率不会有什么特别慢 因为原理是一样的   

第一层foreach就是吧你这个1000多个数组依次处理   里边一层foreach是每个数组具体的处理    基本思路就是每一个新的key就压入结果数组 老的key就替换对应值 最终的数组有所有的key  并且每个key只有一个值


好 知道了  你能贴一下三个数组怎么写么  我是新手   看不太懂你这个方法

多个数组是怎么回事(想当然的?)
如果真的是有桑前的数组需要合并的话,你就得考虑的的思路死否有问题了
如果坚持要事后合并那么应该使用 array_merge_recursive
然后对得到的结果处理一下

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

多个数组是怎么回事(想当然的?)
如果真的是有桑前的数组需要合并的话,你就得考虑的的思路死否有问题了
如果坚持要事后合并那么应该使用 array_merge_recursive
然后对得到的结果处理一下

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

   终于搞好了 谢谢  谢谢

谢谢俩位大牛   辛苦了

谢谢楼上的俩位大牛   辛苦了  分不多  

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

能帮到就好  不用客气

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn