Sorting an array in PHP is a common task in web development. You may need to sort an array based on different criteria, such as alphabetical order, numerical order, or even date order. However, sometimes you may need to sort an array based on custom criteria that don’t fit into any of these categories. In such cases, you can write a custom comparison function to sort the array, per your requirement.
One such custom comparison function that you can use to sort an array by “InProgress” status could be usort_array_by_scheduled_in_progress(). This function takes two arguments: $a and $b, which represent the two elements of the array which is being compared. The function compares the “Status” key of both elements, and if $a has a “Status” value of “InProgress” and $b doesn’t, it returns -1. If $b has a “Status” value of “InProgress” and $a doesn’t, it returns 1. If both elements have the same “Status” value or neither has a “Status” key, it returns 0.
Here is an example of how you can use the usort_array_by_scheduled_in_progress() function to sort an array:
function usort_array_by_scheduled_in_progress( $a, $b ) {
if ( $a[ 'Status' ] == 'InProgress' && $b[ 'Status' ] != 'InProgress' ) {
return -1;
} else if ( $a[ 'Status' ] != 'InProgress' && $b[ 'Status' ] == 'InProgress' ) {
return 1;
} else {
return 0;
}
}
$my_array = array(
array('Name' => 'Task A', 'Status' => 'InProgress'),
array('Name' => 'Task B', 'Status' => 'Completed'),
array('Name' => 'Task C'),
array('Name' => 'Task D', 'Status' => 'InProgress'),
array('Name' => 'Task E', 'Status' => 'Scheduled')
);
usort($my_array, 'usort_array_by_scheduled_in_progress');
print_r($my_array);
In this example, we have an array of tasks, each represented as an associative array with a “Name” key and a “Status” key. We use the usort() function to sort the array using usort_array_by_scheduled_in_progress() as the comparison function. The sorted array will have all tasks with “InProgress” status at the beginning, followed by tasks with other statuses (completed or scheduled), and tasks with no status key.
The output of the code will be:
Array
(
[0] => Array
(
[Name] => Task A
[Status] => InProgress
)
[1] => Array
(
[Name] => Task D
[Status] => InProgress
)
[2] => Array
(
[Name] => Task B
[Status] => Completed
)
[3] => Array
(
[Name] => Task E
[Status] => Scheduled
)
[4] => Array
(
[Name] => Task C
)
)
As you can see, the array is sorted based on the “InProgress” status of the tasks, with Task A and Task D at the beginning, followed by Task B and Task E, and finally Task C.
In conclusion, usort_array_by_scheduled_in_progress() is a custom comparison function that you can use to sort an array based on the “InProgress” status of its elements. You can modify this function to sort an array based on any custom criteria that you need.
Show us some ❤️
If you found this article useful and informative, please share it with others who may benefit from it. Also, feel free to leave a comment below with your thoughts or any questions you may have.