Sorting Multidimensional Arrays by a Key
When dealing with complex data structures like multidimensional arrays, it becomes necessary to reorganize their contents according to specific criteria. One scenario involves sorting the array by a key, such as sorting invites by their status.
To achieve this, we utilize a comparison function, which compares values of the key across different array rows. The provided example code defines a function called "cmp" that compares the "status" key values:
Once the comparison function is established, the usort function takes the array and the comparison function as parameters. It sorts the array using the specified comparison criteria, resulting in an array sorted by the "status" key.
The comparison function can be customized to sort on any desired key by modifying the comparison criteria within the function. This approach offers flexibility in adapting the sorting logic to various keys within multidimensional arrays.
The above is the detailed content of How to Sort Multidimensional Arrays by a Specific Key?. For more information, please follow other related articles on the PHP Chinese website!