遇到了个小问题,两个数组合并好快速的写入数据库中,但是怎么合并都不理想,想问下。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;
}