ArrayObject::asort
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
ArrayObject::asort — Sort the entries by value
說明
public ArrayObject::asort(int
$flags
= SORT_REGULAR
): boolSorts the entries in ascending order, such that its keys maintain their correlation with the values they are associated with.
This is used mainly when sorting associative arrays where the actual element order is significant.
注意:
如果兩個成員完全相同,那麼它們將保持原來的順序。 在 PHP 8.0.0 之前,它們在排序陣列中的相對順序是未定義的。
參數
-
flags
-
可選的第二個參數
flags
可以用以下值改變排序的行為:排序型別標記:
-
SORT_REGULAR
- 正常比較單元 詳細描述參見 比較運算子 章節 -
SORT_NUMERIC
- 單元被作為數字來比較 -
SORT_STRING
- 單元被作為字串來比較 -
SORT_LOCALE_STRING
- 根據目前的區域(locale)設定來把單元當作字串比較,可以用 setlocale() 來改變。 -
SORT_NATURAL
- 和 natsort() 類似對每個單元以「自然的順序」對字串進行排序。 -
SORT_FLAG_CASE
- 能夠與SORT_STRING
或SORT_NATURAL
合併(OR 位運算),不區分大小寫排序字串。
-
返回值
總是返回 true
。
範例
示例 #1 ArrayObject::asort() example
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
$fruitArrayObject = new ArrayObject($fruits);
$fruitArrayObject->asort();
foreach ($fruitArrayObject as $key => $val) {
echo "$key = $val\n";
}
?>
以上例程會輸出:
c = apple b = banana d = lemon a = orange
The fruits have been sorted in alphabetical order, and the key associated with each entry has been maintained.
參見
- ArrayObject::ksort() - Sort the entries by key
- ArrayObject::natsort() - Sort entries using a "natural order" algorithm
- ArrayObject::natcasesort() - Sort an array using a case insensitive "natural order" algorithm
- ArrayObject::uasort() - Sort the entries with a user-defined comparison function and maintain key association
- ArrayObject::uksort() - Sort the entries by keys using a user-defined comparison function
- asort() - 對陣列進行升序排序並保持索引關係