在PHP中,可以使用以下函數對數組的鍵值進行排序:
例如,對一個關聯數組進行按鍵名排序:
$fruits = array("apple" => "red", "banana" => "yellow", "cherry" => "red");
ksort($fruits);
foreach ($fruits as $key => $value) {
echo $key . ": " . $value . "\n";
}
以上代碼將輸出:
apple: red
banana: yellow
cherry: red