2016-09-20 52 views
1

另一個陣列有兩個數組:插入一個數組導致多維arrray

$arr1 = array 
(
    [0] => A 
    [1] => B 
    [2] => C 
    [3] => D 
) 

$arr2 = array 
(
    [0] => d 
    [1] => e 
    [2] => f 
) 

我想打一個多維數組會是這個樣子:

$arr3 = array 
(
    [0] => A => array 
      (
       [0] => d 
       [1] => e 
       [2] => f 
      ) 
    [1] => B 
    [2] => C 
    [3] => D 
) 

我想第一個數組的值應該是第二個數組的關鍵。

回答

0

如果你想設置dynamicaly那麼你可以使用:

$arr3 = $arr1; 
$arr3[0] = array($arr3[0] => $arr2); 
1

這應該做的伎倆:

$arr3 = $arr1; 
$arr3[0] = array('A' => $arr2); 
+0

是的,它一個惡作劇。雖然像這樣'$ arr3 [0] = array($ arr3 [0] => $ arr2)'不需要知道第一個數組的選定位置的值。謝啦! – mindSurf

0
$arr1 = array('A','B','C','D'); 
$arr2 = array('d','e','f'); 
$array = $arr1; 
$array[0] = array($arr1[0]=>$arr2); 
var_dump($array); 

請參閱https://eval.in/645288