2016-03-16 40 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