如何將兩個數組有條件的合併成一個二維數組?
P粉925529649
P粉925529649 2023-02-26 10:20:56
0
1
491

遇到了個小問題,兩個數字組合併好快速的寫入資料庫中,但是怎麼合併都不理想,想問下。 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 ) )


#
P粉925529649
P粉925529649

全部回覆 (1)
P粉256198018

$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;

}

    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!