Hướng dẫn dùng php ksort trong PHP

Hàm ksort() sẽ sắp xếp các phần tử của mảng dựa vào khóa(key). Các cặp key => value được giữ nguyên và chúng chỉ được sắp xếp lại theo thứ tự alphabet.

Hướng dẫn dùng php ksort trong PHP

Hướng dẫn dùng php ksort trong PHP

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú phápksort($array, $sort_flag);

Trong đó:

  • $array là mảng cần sắp xếp lại.
  • $sort_flag là tham số không bắt buộc, bạn có thể tham khảo kỹ hơn ở hàm sort() .

Ví dụ

Code

$subject = array(
	"php"=>"1",
	"css"=>"2",
	"js"=>"3",
	"html"=>"4"
);
ksort($subject);
foreach ($subject as $key => $val) {
    echo "$key = $val
"; }

Kết quả

css = 2
html = 4
js = 3
php = 1

Mảng $subject đã được sắp xếp lại dựa vào khóa.

Bài viết này được đăng tại [free tuts .net]

Tham khảo: php.net

Định nghĩa hàm ksort() trong PHP

Hàm ksort() sắp xếp một mảng bởi key, duy trì mối tương quan giữa key và dữ liệu. Hàm này chủ yếu hữu ích cho các mảng liên hợp.

Cú pháp hàm ksort() trong PHP

Hàm ksort() trong PHP có cú pháp như sau:

ksort ( $array, $sort_flag );

Tham số

Tham sốMiêu tả
array Bắt buộc. Xác định một mảng
sort_flag

Tùy ý. Xác định cách sắp xếp mảng. Các giá trị có thể có:

  • SORT_REGULAR − Mặc định. Xem xét value như cũ (không thay đổi kiểu)
  • SORT_NUMERIC − Xem xét value dưới dạng số
  • SORT_STRING − Xem xét value dưới dạng các chuỗi
  • SORT_LOCALE_STRING − Xem xét value dưới dạng các chuỗi, dựa trên các cài đặt nội bộ

Trả về giá trị

Hàm này trả về TRUE nếu thành công, và FALSE nếu thất bại.

Ví dụ minh họa cách sử dụng hàm ksort() trong PHP:

"35","Minh"=>"37","Phuong"=>"43");
   ksort($age);

   foreach($age as $x=>$x_value)
       {
       echo "Key=" . $x . ", Value=" . $x_value;
       echo "
"; } ?>

Lưu chương trình trên trong một file có tên là test.php trong htdocs, sau đó mở trình duyệt và gõ địa chỉ http://localhost:8080/test.php sẽ cho kết quả:

Hướng dẫn dùng php ksort trong PHP

Xem thêm Hàm trong php

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm ksort() trong PHP

Hướng dẫn cách sử dụng hàm ksort() về mảng trong lập trình PHP

Tác dụng của hàm ksort()

The ksort() function sorts an associative array in ascending order, according to the key.

The keys are preserved, i.e. the key-to-value mapping will remain unchanged by the sort operation.

The following table summarizes the technical details of this function.

Return Value:Returns TRUE on success or FALSE on failure.
Version:PHP 4+


Syntax

The basic syntax of the ksort() function is given with:

ksort(array, sort_flags);

The following example shows the ksort() function in action.

"ball", "d"=>"dog", "a"=>"apple", "c"=>"cat");

// Sorting alphabets array
ksort($alphabets);
print_r($alphabets);
?>

Tip: The ksort() and krsort() functions used for sorting associative arrays by key, whereas the asort() and arsort() functions mainly used for sorting associative arrays by value.


Parameters

The ksort() function accepts the following parameters.

ParameterDescription
array Required. Specifies the array to sort.
sort_flags

Optional. Specifies how array items should be compared. Possible values are:

  • SORT_REGULAR – Compare items normally (don't change types). Default value.
  • SORT_NUMERIC – Compare items numerically.
  • SORT_STRING – Compare items as strings.
  • SORT_LOCALE_STRING – Compare items as strings, based on the current locale.
  • SORT_NATURAL – Compare items as strings using natural ordering.
  • SORT_FLAG_CASE – Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.


More Examples

Here're some more examples showing how ksort() function actually works:

The following example sorts the "persons" associative array by key in ascending order:

18, "Clark"=>32, "Peter"=>20, "John"=>24);

// Sorting persons array
ksort($persons);
print_r($persons);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới