2012-07-14 82 views

回答

9

是的,它確實保存了訂單。你可以把php數組想象成ordered hash maps

您可以將元素視爲按「索引創建時間」排序。例如

$a = array(); 
$a['x'] = 1; 
$a['y'] = 1; 
var_dump($a); // x, y 

$a = array(); 
$a['x'] = 1; 
$a['y'] = 1; 
$a['x'] = 2; 
var_dump($a); // still x, y even though we changed the value associated with the x index. 

$a = array(); 
$a['x'] = 1; 
$a['y'] = 1; 
unset($a['x']); 
$a['x'] = 1; 
var_dump($a); // y, x now! we deleted the 'x' index, so its position was discarded, and then recreated 

總之,如果你加入其中的關鍵犯規當前存在的數組中的一個條目,該條目的位置將是列表的末尾。如果您正在更新現有密鑰的條目,則位置不變。

foreach使用上面演示的自然順序在數組上循環。如果你喜歡,你也可以使用next()current()prev()reset()和朋友,儘管自從foreach被引入語言以後,它們很少被使用。

另外,print_r()和var_dump()也使用自然數組順序輸出結果。

如果你對java很熟悉,LinkedHashMap是最相似的數據結構。

相關問題