2012-07-14 81 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是最相似的数据结构。

相关问题