二维数组需要合并
$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', ),)
你可以用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));
能帮到就好 不用客气