2011-03-03 58 views
1

我有兩個匹配鍵的數組,我需要將兩者的值合併到一個新的數組中。我正在努力弄清楚這一點。我怎樣才能做到這一點?合併兩個數組,其中密鑰匹配

$options = array(
    "0" => true, 
    "1" => true, 
    "2" => false, 
    "3" => true 
); 

$columns = array(
    "0" => "first", 
    "1" => "last", 
    "2" => "id", 
    "3" => "group" 
); 

$what_I_need = array(
    "first" => true, 
    "last" => true, 
    "id" => false, 
    "group" => true 
); 

回答

7
$whatYouNeed = array_combine($columns, $options); 

這是假設的陣列是正確的順序。否則:

$whatYouNeed = array(); 
foreach ($columns as $key => column) { 
    $whatYouNeed[$column] = $options[$key]; 
} 
+0

$ whatYouNeed!你贏了我的what_i_need! – Peter

+0

@彼得大聲笑,我有一個很好的笑出來它 – kjy112

+1

不公平,我是第一個:( – 2011-03-03 02:48:43

2
$what_I_need = array_combine($columns , $options); 

array_combine

2
array array_combine (array $keys , array $values); 

$newArray = array_combine($columns, $options); 
print_r($newArray); 

應該給你:

array(
    "first" => true, 
    "last" => true, 
    "id" => false, 
    "group" => true 
); 

PHP array_combine

相關問題