2016-06-08 108 views
-1

我有以下的PHP:PHP添加多個對象數組

$refined_results = array(); 

    $refined_results['First'] = $results[0];   
    $refined_results['Second'] = $results[1]; 
    $refined_results['Third'] = $results[2]; 
    $refined_results['Fourth'] = $results[3]; 
    $refined_results['Fifth'] = $results[4]; 
    $refined_results['Sixth'] = $results[5]; 

有沒有辦法簡化這個?

例如(完全錯誤的,當然):

$refined_results['First','Second','Third'] = $results[0,1,2]; //Yup... 
+2

你爲什麼要叫它第一,第二,第三...... ? – nospor

+0

這是我給的任意名稱。查詢結果有我需要更改的名稱。 –

回答

3

可以使用array_combine功能:

$keys = ['first', 'second', 'third', 'fourth', 'fifth']; 
$refined_results = array_combine($keys, $results); 
+0

我從你的答案提出了另一個問題:http://stackoverflow.com/questions/37713683/php-targeting-specific-keys-in-array-combine –

1
$refined_results = array(); 
$keys = array('First', 'second', 'third');// you can add more 
foreach($keys as $i=>$k) { 
$refined_results[$k] = $results[$i]; 
}