合并两个多维关联数组



我有这两个数组,我想合并成一个数组,但一直很难弄清楚。下面是这两段代码:

第一个数组:

$propSelectMaxRow = max(array_keys($property_select_email));
$i = 0;
foreach($property_queue as $r) {
    if ($select_type == 3 || $select_type == 4 || $select_type == 5) {
        $i = $i + 1;
        $property_select_email[$propSelectMaxRow + $i] = $r;
    }
}

第二个数组:

$propSelectMaxRow = max(array_keys($property_select_email));
$i = 0;
foreach($property_select as $r) {
    if ($select_type == 1 || $select_type == 2 || $select_type == 4 || $select_type == 5) {
        $i = $i + 1;
        $property_select_email[$propSelectMaxRow + $i] = $r;
    }
}

编辑:我希望最后的数组看起来像这样:

第一个数组格式:

Array ( 
    [1] => Array ( [code] => PPJ3 [street] => 34412 Fake Street [city] => Detroit [state] => MI [zip] => 48223 ) 
    [2] => Array ( [code] => PLK3 [street] => 73517 Fake Street [city] => Detroit [state] => MI [zip] => 48223 ) 
    [3] => Array ( [code] => HYK2 [street] => 55224 Fake Street [city] => Detroit [state] => MI [zip] => 48208 ) 
 )

第二个数组格式:

Array ( 
    [1] => Array ( [code] => JAK932 [street] => 353242 Fake Street [city] => Detroit [state] => MI [zip] => 48223 ) 
    [2] => Array ( [code] => JA232 [street] => 7432 Fake Street [city] => Detroit [state] => MI [zip] => 48223 ) 
 )   

最终数组格式应为:

Array ( 
    [1] => Array ( [code] => PPJ3 [street] => 34412 Fake Street [city] => Detroit [state] => MI [zip] => 48223 ) 
    [2] => Array ( [code] => PLK3 [street] => 73517 Fake Street [city] => Detroit [state] => MI [zip] => 48223 ) 
    [3] => Array ( [code] => HYK2 [street] => 55224 Fake Street [city] => Detroit [state] => MI [zip] => 48208 ) 
    [4] => Array ( [code] => JAK932 [street] => 353242 Fake Street [city] => Detroit [state] => MI [zip] => 48223 ) 
    [5] => Array ( [code] => JA232 [street] => 7432 Fake Street [city] => Detroit [state] => MI [zip] => 48223 ) 
 )

您可能需要像这样改进您的代码:

//$propSelectMaxRow = max(array_keys($property_select_email)); // no need
//$i = 0; //no need
foreach($property_queue as $r) {
    if ($select_type == 3 || $select_type == 4 || $select_type == 5) {
        //$i = $i + 1;
        $property_select_email[] = $r;
    }
}

同样,构建第二个数组。第二个循环将在第一个循环的末尾添加数组(因此它们将自动合并)。

看起来你只需要使用array_merge

相关内容

  • 没有找到相关文章

最新更新