遇到了個小問題,兩個數字組合併好快速的寫入資料庫中,但是怎麼合併都不理想,想問下。 1和2數組同一個key的組成一起,並且把key修改為array1和raary2,各位幫看下,謝謝了
//数组1 $arry1=array ( [0] => 1-ASD [1] => 1-EWFEEF [2] => 1-BHRTBHTR [3] => 1-RTHJT [4] => 1-TNJRTHTR ) //数组1 $array2=array ( [0] => 2-ASD [1] => 2-EWFEEF [2] => 2-BHRTBHTR [3] => 2-RTHJT [4] => 2-TNJRTHTR ) //想要输出的二维数组形式 //1和2数组同一个key的组成一起,并且把key修改为array1和raary2 //新数组 $array=array ( [0] => Array ( [array1] => 1-ASD [array2] => 2-ASD ) [1] => Array ( [array1] => 1-EWFEEF [array2] => 1-EWFEEF ) [2] => Array ( [array1] => 1-BHRTBHTR [array2] => 2-BHRTBHTR ) [3] => Array ( [array1] => 2-TNJRTHTR [array2] => 2-TNJRTHTR ) [4] => Array ( [array1] => 2-KMYTY [array2] => 2-KMYTY ) )
解
$new_arr = [];
foreach($array1 as $k1=>$v1){
foreach($arrat2 as $k2 = > $v2){
if($k1 == $k2){ # $new_arr[$k1]['array1'] = $v1; $new_arr[$k1]['arrat2'] = $v2; } } 返回$new_arr;
}