PHP

PHP TUTORIAL: Sort Array by Key in PHP with Examples

Description:

Sorting arrays is a common task in programming, and in PHP, there are several functions available to sort arrays by value. However, sometimes you may need to sort an array by its keys instead. In this article, we’ll discuss how to sort an array by key in PHP and provide some examples to help you understand the process.




Using ksort() to Sort an Array by Key in PHP

The ksort() function is used to sort an array by its keys in ascending order. Here’s an example of how to use ksort() to sort an array by key:

output:

Code Explanation:

An array called $fruits is defined, which contains three key-value pairs representing the quantity of each fruit. The keys are the names of the fruits (“banana”, “apple”, and “orange”), and the values are their respective quantities (2, 1, and 3).

The ksort() function is called with $fruits as its argument. This function sorts the array by its keys in ascending order.

A foreach loop is used to iterate over the sorted array. For each element in the array, the loop assigns the key to $key and the value to $value.

Inside the loop, an echo statement is used to print out the key and value of each element, separated by an equal sign and followed by a newline character (\n). The echo statement uses double quotes to allow variable interpolation, so the values of $key and $value are substituted into the string.

The loop continues until all elements in the array have been processed.



Using krsort() to Sort an Array by Key in PHP

If you want to sort an array by key in descending order, you can use the krsort() function. Here’s an example of how to use krsort() to sort an array by key:

output:

Code Explanation:

An array called $fruits is defined, which contains three key-value pairs representing the quantity of each fruit. The keys are the names of the fruits (“banana”, “apple”, and “orange”), and the values are their respective quantities (2, 1, and 3).

The krsort() function is called with $fruits as its argument. This function sorts the array by its keys in descending order.

A foreach loop is used to iterate over the sorted array. For each element in the array, the loop assigns the key to $key and the value to $value.

Inside the loop, an echo statement is used to print out the key and value of each element, separated by an equal sign and followed by a newline character (\n). The echo statement uses double quotes to allow variable interpolation, so the values of $key and $value are substituted into the string.

The loop continues until all elements in the array have been processed.



Using uksort() to Sort an Array by Key in PHP

If you need to sort an array by key using a custom sorting function, you can use the uksort() function. Here’s an example of how to use uksort() to sort an array by key using a custom sorting function:

output:

Code Explanation:

First, a function called compare_keys() is defined. This function takes two arguments, $key1 and $key2, which are the keys of the elements being compared.

The function checks whether $key1 and $key2 are equal. If they are, it returns 0 to indicate that the keys are the same.

If $key1 and $key2 are not equal, the function uses a ternary operator to return either -1 or 1. This depends on whether $key1 is less than or greater than $key2.

Next, an array called $fruits is defined. This array contains three key-value pairs, where the keys are the names of fruits and the values are their quantities.

The uksort() function is called with two arguments: $fruits and the name of the comparison function, compare_keys(). This function sorts the array $fruits by its keys using the compare_keys() function as the sorting criteria.

Finally, a foreach loop is used to iterate over the sorted array. For each element in the array, the loop assigns the key to $key and the value to $value. The echo statement then prints out the key-value pair, separated by an equal sign.



Sorting arrays by key is a common task in PHP development, and the ksort() and krsort() functions make it easy and efficient to do so. Whether you need to sort an array by key in ascending or descending order, these functions can help you achieve your desired result quickly and easily.

In summary, to sort an array by key in PHP, you can use the ksort() function to sort the array in ascending order or the krsort() function to sort the array in descending order. Both functions modify the array directly, and you can then use a foreach loop to iterate over the sorted array and access its key-value pairs.

So that’s how you can sort an array by key in PHP. We hope you found this article useful, and that it helps you with your PHP development projects!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button