2016-03-16 71 views
0

我得到了以下O/P在PHP如何分配特定陣列鍵的主陣列

$test1=["12","54","87","98"]; 
$test2=["1","5","8","9"]; 

在這如何可以在連接上述兩個變量的一個變量像下面

1是12的鍵, 5是54鍵, 8是87鍵, 9是98鍵,

+0

使用'array_combine()'。 – Yash

+0

['array array_combine(array $ keys,array $ values)'](http://php.net/manual/en/function.array-combine.php) – Sean

+0

謝謝你,親愛的,它正在工作 – KarnaGowtham

回答

2

嘗試這種情況:

$test1=["12","54","87","98"]; 

$test2=["1","5","8","9"]; 

$a = array_combine($test2, $test1); 

echo "<pre>"; 

print_r($a); 

exit; 

希望這會有所幫助。

和平!的xD

+0

謝謝你,親愛的,它工作好 – KarnaGowtham

1

你可以試試這個

$test1=["12","54","87","98"]; 
$test2=["1","5","8","9"]; 


$new_array = array(); 

$i = 0; 
foreach($test2 as $key=> $value) 
{ 
    $new_array[$value] = $test1[$i]; 
    $i++; 
} 

echo '<pre>'; 
print_r($new_array); 
+0

array_combine最好親愛的 – KarnaGowtham

+0

我這麼認爲是因爲它內置的功能。@ KarnaGowtham –

+0

好吧好吧親愛的,我找出解決方案 – KarnaGowtham