• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Php Tip: Use sort() to sort an array

December 19, 2023

Php Tip: Use sort() to sort an array

Sorting arrays is a common task in PHP programming. Whether you need to organize a list of names, sort numbers in ascending or descending order, or arrange data based on specific criteria, the sort() function in PHP can be a handy tool. In this article, we will explore how to use the sort() function effectively to sort arrays in PHP.

Understanding the sort() function

The sort() function in PHP is a built-in function that allows you to sort an array in ascending order. It rearranges the elements of the array in place, meaning it modifies the original array directly. The sort() function works with both indexed and associative arrays.

Here’s the basic syntax of the sort() function:

sort($array);

Where $array is the array you want to sort.

Sorting indexed arrays

Let’s start by sorting an indexed array. Consider the following example:

$fruits = array("apple", "banana", "orange", "grape");
sort($fruits);

After executing the sort() function, the $fruits array will be sorted in ascending order:

Array
(
    [0] => apple
    [1] => banana
    [2] => grape
    [3] => orange
)

If you want to sort the array in descending order, you can use the rsort() function instead:

$fruits = array("apple", "banana", "orange", "grape");
rsort($fruits);

The rsort() function will produce the following result:

Array
(
    [0] => orange
    [1] => grape
    [2] => banana
    [3] => apple
)

Sorting associative arrays

The sort() function can also be used to sort associative arrays based on their values. Consider the following example:

$students = array(
    "John" => 85,
    "Jane" => 92,
    "Mark" => 78,
    "Emily" => 95
);
asort($students);

After executing the asort() function, the $students array will be sorted in ascending order based on the values:

Array
(
    [Mark] => 78
    [John] => 85
    [Jane] => 92
    [Emily] => 95
)

If you want to sort the array based on the keys instead, you can use the ksort() function:

$students = array(
    "John" => 85,
    "Jane" => 92,
    "Mark" => 78,
    "Emily" => 95
);
ksort($students);

The ksort() function will produce the following result:

Array
(
    [Emily] => 95
    [Jane] => 92
    [John] => 85
    [Mark] => 78
)

Conclusion

The sort() function in PHP is a powerful tool for sorting arrays. Whether you need to sort indexed arrays in ascending or descending order or sort associative arrays based on values or keys, the sort() function can handle it all. By understanding how to use the sort() function effectively, you can easily organize and arrange your array data in PHP.

Summary

In conclusion, the sort() function in PHP is a versatile tool for sorting arrays. It can be used to sort both indexed and associative arrays based on various criteria. By mastering the sort() function, you can efficiently organize and arrange your array data in PHP.

For more information about VPS hosting services, visit Server.HK.

Recent Posts

  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot