二次元配列はマージする必要があります
$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" ));
$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" ));
この配列は数千あるため、より高速な方法を使用するようにしてください
2 つの配列が同じサイズで順序が同じ場合、
$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', ),)
フィールドが繰り返され、最後のものであると仮定すると、どのように対処しますか?が取られ、処理対象の配列がすべて同じキーを持つ 2 次元配列である場合、次のように書くことができます
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 ))
2 つの配列が同じサイズ、同じ順序の場合、
$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', ),)
2 つの配列が同じサイズで順序が同じであれば、
$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', ),)
原理が同じであるため、私の 2 つの foreach の効率は特に遅くはありません
foreach の最初の層は、1,000 を超える配列で処理されますシーケンスの場合、foreach の内部層は各配列の特定の処理です。基本的な考え方は、新しいキーが結果の配列にプッシュされ、古いキーが対応する値に置き換えられることです。最終的な配列にはすべてのキーが含まれます。各キーには値が 1 つだけあります
原則が同じであるため、私の 2 つの foreach の効率は特に遅くなりません
foreach の最初の層は次の処理を行うことですforeach の内部層は、各配列の特定の処理です。基本的な考え方は、新しいキーが結果の配列にプッシュされ、古いキーが対応する値に置き換えられることです。キーと各キーには 1 つの値しかありません
わかりました。3 つの配列を記述する方法を投稿してもらえますか? 私は初心者なので、その方法がわかりません
どうしても後でマージしたい場合は、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 ))
上の階の偉い人 2 人、お疲れ様でした。
merge_arr(array($arr2,$arr,$arr3,$arr4,$arr5,...,$arr1000) );
どういたしまして。