2010-08-24 68 views
0

一個陣列我有兩個數組:PHP數組從兩個

$ids: 

    Array 
(
    [0] => 1 
    [1] => 2 
    [2] => 3 
    [3] => 4 
    [4] => 8 
) 

$ranks: 

Array 
(
    [0] => Rank1 
    [1] => Rank2 
    [2] => Rank3 
    [3] => Rank5 
    [4] => NoName 
) 

如何創建從這兩個數組,這樣array('1'=>'Rank1','2'=>'Rank2','3'=>'Rank3','4'=>'Rank5','8'=>'Noname')

回答

10

使用array_combine()功能,它允許你映射的在一個關聯數組中的鍵值數組。

$ids_ranks = array_combine($ids, $ranks); 
print_r($ids_ranks); 

輸出:

Array 
(
    [1] => Rank1 
    [2] => Rank2 
    [3] => Rank3 
    [4] => Rank5 
    [8] => NoName 
) 
+0

謝謝,這是作品。 – pltvs 2010-08-24 10:01:21