Type Here to Get Search Results !
×
Hi ,

We always try to serve you better. Please enter the institute name, you belong to.

×
Hello ,

Your daily download limit has been exhausted. Share it with your friends to download more.

How to sort multidimensional array in PHP

Sort multidimensional array in PHP

<?php
// Example multidimensional array
$data = array(
    array(
        "id" => 1,
        "name" => "John",
        "age" => 25
    ),
    array(
        "id" => 2,
        "name" => "Mike",
        "age" => 30
    ),
    array(
        "id" => 3,
        "name" => "Tina",
        "age" => 23
    )
);

// Sort the array by the "age" key
usort($data, function($a, $b) {
    return $a['age'] <=> $b['age'];
});

// Print the sorted array
print_r($data);


You can also sort it by using array_multisort() function.


<?php
// Example multidimensional array
$data = array(
    array(
        "id" => 1,
        "name" => "John",
        "age" => 25
    ),
    array(
        "id" => 2,
        "name" => "Mike",
        "age" => 30
    ),
    array(
        "id" => 3,
        "name" => "Tina",
        "age" => 23
    )
);

// Sort the array by the "age" key
array_multisort(array_column($data, 'age'), SORT_ASC, $data);

// Print the sorted array
print_r($data);

Here, array_column() is used to extract the values of a single column from the multi-dimensional array, and then sort that array using array_multisort(). The resulting sorted array is then used to sort the original multi-dimensional array.


Sort Multidimensional Array PHP by two keys.

You can use the array_multisort() function to sort a multidimensional array by multiple keys. Here's an example of how you can sort a multidimensional array by two keys, "age" and "name":

<?php
// Example multidimensional array
$data = array(
    array(
        "id" => 1,
        "name" => "John",
        "age" => 25
    ),
    array(
        "id" => 2,
        "name" => "Mike",
        "age" => 30
    ),
    array(
        "id" => 3,
        "name" => "Tina",
        "age" => 23
    )
);

// Extract the values of the "age" and "name" keys
$age = array_column($data, 'age');
$name = array_column($data, 'name');

// Sort the arrays by the "age" and "name" keys
array_multisort($age, SORT_ASC, $name, SORT_ASC, $data);

// Print the sorted array
print_r($data);


Here, array_column() is used to extract the values of the "age" and "name" keys. These arrays are then sorted using array_multisort(). The resulting sorted arrays are then used to sort the original multi-dimensional array, $data.

You can also use usort() function with your custom comparision function to sort by multiple keys.


usort($data, function($a, $b) {
    if ($a['age'] == $b['age']) {
        return strcmp($a['name'], $b['name']);
    }
    return $a['age'] <=> $b['age'];
});


In this example, first it checks if age is same for both elements, if yes then it compares the name, otherwise it compares the age.


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.