Home > Backend Development > PHP Tutorial > How to Custom Sort Multidimensional Arrays Based on a Specific Key in PHP?

How to Custom Sort Multidimensional Arrays Based on a Specific Key in PHP?

Linda Hamilton
Release: 2024-10-20 19:04:02
Original
748 people have browsed it

How to Custom Sort Multidimensional Arrays Based on a Specific Key in PHP?

Custom Sorting of Multidimensional Arrays

Sorting multidimensional arrays can be a common task in programming. This article demonstrates how to sort such an array based on a specific key using the PHP usort() function.

Understanding the Task

Consider the following multidimensional array where each element represents a record:

<code class="php">Array (
    [0] => Array
        (
            [iid] => 1
            [invitee] => 174
            [nid] => 324343
            [showtime] => 2010-05-09 15:15:00
            [location] => 13
            [status] => 1
            [created] => 2010-05-09 15:05:00
            [updated] => 2010-05-09 16:24:00
        )

    [1] => Array
        (
            [iid] => 1
            [invitee] => 220
            [nid] => 21232
            [showtime] => 2010-05-09 15:15:00
            [location] => 12
            [status] => 0
            [created] => 2010-05-10 18:11:00
            [updated] => 2010-05-10 18:11:00
        ))</code>
Copy after login

Sorting by a Key

To sort this array by the "status" key, we need to define a comparison function that determines the order of the elements.

<code class="php">function cmp($a, $b) {
    if ($a['status'] == $b['status']) {
        return 0;
    }
    return ($a['status'] < $b['status']) ? -1 : 1;
}</code>
Copy after login

This function compares the "status" values of two elements. If the values are equal, it returns 0. If the first element has a lower "status" value than the second, it returns -1. Otherwise, it returns 1.

Using usort()

We can now use the usort() function to sort the array using our comparison function:

<code class="php">usort($array, "cmp");</code>
Copy after login

This will sort the array in ascending order based on the "status" key. The modified array will have its elements rearranged accordingly.

Customizing the Comparison

The comparison function can be customized to sort the array based on any key. Simply modify the function to compare the desired key values. This provides flexibility in sorting multidimensional arrays based on different criteria.

The above is the detailed content of How to Custom Sort Multidimensional Arrays Based on a Specific Key in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template