2014-03-13 108 views
1
$employeeAges = array(); 
$employeeAges["Lisa"] = "28"; 
$employeeAges["Jack"] = "16"; 
$employeeAges["Ryan"] = "35"; 
$employeeAges["Rachel"] = "46"; 
$employeeAges["Grace"] = "34"; 
sort($employeeAges); 
foreach($employeeAges as $name => $age){ 
    echo "Name: $name, Age: $age <br />"; 
} 

當我的$ employeeAges它打印出這個排序,沒有姓名的員工排序PHP中的數組

Name: 0, Age: 16 
Name: 1, Age: 28 
Name: 2, Age: 34 
Name: 3, Age: 35 
Name: 4, Age: 46 

我將如何得到它打印出來的名字呢?我不知道它爲什麼會改變:S

+2

簡單'asort' http://php.net/asort – cmorrissey

+0

cmorrissey解決了它!謝謝,完全忘記了asort! – user3364498

+0

歡迎來到Stack Overflow。請在查詢之前確保搜索。 – Boaz

回答

1

使用asort($array);

它會爲你做的工作。