Home > Backend Development > PHP Tutorial > How to Sort Multi-Dimensional Arrays Based on Inner Array Value in PHP?

How to Sort Multi-Dimensional Arrays Based on Inner Array Value in PHP?

Barbara Streisand
Release: 2024-11-03 21:30:29
Original
488 people have browsed it

How to Sort Multi-Dimensional Arrays Based on Inner Array Value in PHP?

Sorting Multi-Dimensional Arrays Based on Inner Array Value in PHP

Arrays in PHP provide a powerful data structure for organizing and storing data. However, sorting multi-dimensional arrays based on a specific value within an inner array can be a challenging task.

Problem Description

Consider a PHP hashtable with the following data structure:

[
    [
        "type" => "suite",
        "name" => "A-Name"
    ],
    [
        "type" => "suite",
        "name" => "C-Name"
    ],
    [
        "type" => "suite",
        "name" => "B-Name"
    ]
]
Copy after login

The goal is to sort this data structure based on the "name" key in the inner array.

Sorting Methods

Various PHP functions can be used for sorting arrays:

  • ksort: Sorts associative arrays (hashtables) by key.
  • sort: Sorts arrays with values.
  • usort: Sorts arrays using a user-defined comparison function.

However, none of these functions directly support sorting based on a key within an inner array.

Custom Sorting Function

To overcome this limitation, a custom sort function can be defined to compare the desired values within the inner array. This can be accomplished using the usort function.

<code class="php">function cmp($a, $b) {
    return $b['name'] - $a['name'];
}</code>
Copy after login

This function compares the "name" key in the inner arrays and returns a negative value if the first array's name is greater than the second array's name.

Sorting the Array

Once the comparison function is defined, it can be used to sort the array using the usort function.

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

An Alternative Solution

An alternative solution to the custom sort function is to create a new array that contains only the values to be sorted. This can be done with a nested loop and a conditional statement.

<code class="php">function array_sort($array, $on, $order=SORT_ASC) {
    // ...
    return $new_array;
}</code>
Copy after login

This function takes in the array to be sorted, the key to sort on, and an optional order (ascending or descending). It returns a new array with the sorted data.

Usage

The array_sort function can be used as follows:

<code class="php">$mydata = [
    ['type' => 'suite', 'name' => 'A-Name'],
    ['type' => 'suite', 'name' => 'C-Name'],
    ['type' => 'suite', 'name' => 'B-Name']
];

$sorted_data = array_sort($mydata, 'name', SORT_ASC);

print_r($sorted_data);</code>
Copy after login

The above is the detailed content of How to Sort Multi-Dimensional Arrays Based on Inner Array Value in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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