PHP usort()

WBOY
Release: 2024-08-29 13:05:13
Original
1016 people have browsed it

This function is used to sort an array in PHP. We can sort our array based on our logic and requirement, which means it sorts the defined array with a user-defined function. Also, it removes all the old keys from the array and assigns new keys to every element of the array. These new keys will be an integer that will start from 0 to the number of elements present in the array.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

This function takes two parameters as input. Anyways, PHP has many built-in functions that can be used to sort the array elements, but here, we can pass a user-defined function to sort our array element. This is one of the plus points of this function. Let’s discuss the syntax in detail. See below;

e.g. :

boolean usort( array, function)
Copy after login

As we can see, this function takes two parameters as the input. One is the array that we want to sort and the one is the user-defined function, which will decide the sorting of array elements; in short, how it would behave. Now we will see one practice example to understand the syntax in the better way possible.

e.g. :

usort( $myArr, "demo_sort_function");
Copy after login

In the above syntax, we are passing array and user-defined functions to the usort() function. We will discuss the function in detail in the coming section.

How does usort() Function work in PHP?

As of now, we know that usort() function is used to sort the array in a user-defined manner. We can define our own logic for sorting array elements. PHP comes up with so many built-in functions which sort an array, but one benefit here is we can write our sorting algorithm here to sort. Also, this function will remove all the old keys of the array elements and assign each of them a new key. This key ordering will start from 0 and so on until the length of the function. Now we will discuss in detail the parameters this function takes as input and also the return type of the function as well. see below

This function takes two parameters which are as follows:

1) $array (first param): This is the first parameter this function takes as input. We can define an array and pass it here. This will be the array we want to sort in some new manner.

2) comparision_function (second param): This is the second parameter that this function takes as an argument, and this will be the user-defined function containing the sorting logic with it. We can define one function and call the user-defined function from this usort() function. We need to pass the name in the “function_name” like this manner. Below we are calling one function where we are sorting our array on the basis of name.

e.g. :

usort($arr, "sortByName")
Copy after login

3) Boolean (return type): Return type for this function is Boolean, and Boolean represents the value either TRUE or FALSE depending upon the result of the function. This function will return FALSE in failure cases and TRUE in the success case. Now we will see one practice example of how this works in actuality and where we can use this function. Here is the practice example for beginners to understand its flow and working. See below;

e.g. :

function my_function_to_sort($a1,$b1)
{
if ($a1==$b1) return 0;
if ($a1 < $b1)
return 1;
else
return -1;
}
$myarr=array(40, 50, 20, 10, 100, 500, 20, 18);
usort($myarr,"my_function_to_sort");
Copy after login

In the above example, we have created one array called ‘myarr’, which will contain all the elements which we want to sort. Here we have assigned some values to this array as 40, 50, 20, 10, 100, 500, 20, 18, and used the array function, which is already available in PHP. Also, we have created one function here called as ‘my_function_to_sort’ this function is responsible to sort the array elements base on the condition that we have returned. Inside this, we have mainly written the logic if two values passed are greater than or smaller than, or equal to each other. On the basis of it wit will place the elements into the array.

Now we will see the conditions in detail. What are the significance of 1, -1, and 0 here? Let’s discuss them in detail. See Below;

1) 0: If both the elements are equal then it will return 0 here.

2) 1: From the above condition we can see that if a1 is smaller than b1 then it will return 1.

3) -1: From the above condition, we can see that if a1 is greater than b1 then it will return -1.

And the whole logic will be responsible to sort the array of an integer into the ascending or descending order. In the above program, it will do descending sorting of the array elements for us.

After this, we are calling usort() function, and here we pass our own parameters here, and that is named ‘$myarr’ ‘my_function_to_sort’. This we can say a custom sorting that we have implemented by using the usort ()
function.

Examples of PHP usort()

Lets us discuss the examples of PHP usort().

Example 1

1) In this example, we are sorting an array of an integer using usort() function available in PHP. This is a custom sort, and we are sorting in descending order.




Demo for usort() funciton in PHP

******************** START ***************

"; echo "

values are ::

"; echo "
"; $lenarr=count($myarr); for($i=0;$i<$lenarr;$i++) { echo "Inside loop printing values :::"; echo $myarr[$i]; echo "
"; } ?>

******************** END ***************

Copy after login

Output:

PHP usort()

Example 2

In this example, we are trying to sort an array of the string by using the usort() function available in PHP. Here we are sorting in ascending order or natural order.

<!DOCTYPE html>
<html>
<body>
<p>Demo for usort() funciton in PHP</p>
<p>******************** START ***************</p>
<?php
function my_function_to_sort($a1,$b1)
{
if ($a1==$b1) return 0;
if ($a1 < $b1)
return -1;
else
return 1;
}
$myarr=array("red","yellow", "orange", "black","pink","purple","brown");
usort($myarr,"my_function_to_sort");
echo "<br>";
echo "<p>values are ::</p>";
echo "<br>";
$lenarr=count($myarr);
for($i=0;$i<$lenarr;$i++)
{
echo "Inside loop printing values :::";
echo $myarr[$i];
echo "<br>";
}
?>
<p>******************** END ***************</p>
</body>
</html>
Copy after login

Output:

PHP usort()

Conclusion

By using this function, we can sort the array in a custom order, or we can say, in a user-defined manner. This can be called a new manner sorting. By using this, we can sort any number of elements in the array and apply our own logic to sort the data in ascending and descending or natural order.

The above is the detailed content of PHP usort(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template