Can I Sort sub-array by its value alphabetically in php?

244 views Asked by At

I have $mainArr array and I want result as $resultArr.

$mainArr = array(
            0 => array(125 => 'B', 127 => 'A', 178 => 'Z', 78 => 'G'),
            1 => array(111 => 'X', 127 => 'K', 108 => 'J', 708 => 'P'),
            2 => array(125 => 'L', 127 => 'M', 178 => 'Q', 78 => 'A'),
    );

I want result like below:

$resultArr = array(
        0 => array(127 => 'A', 125 => 'B', 78 => 'G', 178 => 'Z', ),
        1 => array(108 => 'J', 127 => 'K', 708 => 'P', 111 => 'X',),
        2 => array(78 => 'A', 125 => 'L', 127 => 'M', 178 => 'Q'),
    );
6

There are 6 answers

2
Radu Gheorghies On

Of course you can.

You can do it like this:

$resultArr = [];
foreach ($mainArr as $m){
    asort($m);
    $resultArr[] = $m;
}
var_dump($resultArr);

Have a nice day.

0
Narendra Vaghela On

You can use array_map function to sort multi dimensional arrays.

To sort your above array, you can use something like this,

function sortArr($a) {
    natsort($a);
    return $a;
}

$mainArr = array(0 => array(125 => 'B', 127 => 'A', 178 => 'Z', 78 => 'G'),
            1 => array(111 => 'X', 127 => 'K', 108 => 'J', 708 => 'P'),
            2 => array(125 => 'L', 127 => 'M', 178 => 'Q', 78 => 'A'),
    );

$sortedArr = array_map( "sortArr", $mainArr);

It will give below output

Array
(
    [0] => Array
        (
            [127] => A
            [125] => B
            [78] => G
            [178] => Z
        )

    [1] => Array
        (
            [108] => J
            [127] => K
            [708] => P
            [111] => X
        )

    [2] => Array
        (
            [78] => A
            [125] => L
            [127] => M
            [178] => Q
        )

)
0
ajiang On

Here's an answer on StackOverflow that seems to be just what you're looking for: sorting a multi-dimensional array in PHP. It requires defining a custom comparison function and passing it (and the array you wish to sort) as arguments to uasort, which maintains the association between key-value pairs. Note: since you want to sort each row, you would probably sort each row, like this:

function cmp($a, $b) {
    return strcmp($a, $b); // also look into strnatcmp()

function sortArray($mainArr) {
    $resultArr = array();
    foreach (array $arr in $mainArr)
        $resultArr[] = uasort($arr, 'cmp'); // append the new row to $resultArr
    return $resultArr;
}

For future reference, it's useful to google extensively (a skill worth learning, especially in this field) before asking your question (as Burak mentioned). This way, people can provide more effective feedback and suggestions.

0
RiggsFolly On

You can try this, its a bit mandraulic but its easy to understand

$resultArr = array();

foreach ( $mainArr as $sortme ) {
    asort($sortme);
    $resultArr[] = $sortme;

}
2
Saeven On
$mainArr = array_map( function( $a ){
    uksort( $a, function( $b, $c ) use( $a ){
        return $a[$b] > $a[$c];
    });
    return $a;
}, $mainArr );
0
mickmackusa On

You can use array_walk() to traverse the first level and modify the subarrays by reference with&, then call asort() on each subarray to sort alphabetically while preserving the original keys.

No return is necessary because all modifications are applied directly on the input variable.

Code: (Demo)

$mainArr = [
    [125 => 'B', 127 => 'A', 178 => 'Z', 78 => 'G'],
    [111 => 'X', 127 => 'K', 108 => 'J', 708 => 'P'],
    [125 => 'L', 127 => 'M', 178 => 'Q', 78 => 'A'],
];

array_walk($mainArr, function(&$subarray) {
    asort($subarray);
});

var_export($mainArr);

Or the more concise equivalent technique using PHP7.4's arrow function syntax (the return value provided my the magic => is silently disregarded by array_walk()): (Demo)

array_walk($mainArr, fn(&$subarray) => asort($subarray));

Output:

array (
  0 => 
  array (
    127 => 'A',
    125 => 'B',
    78 => 'G',
    178 => 'Z',
  ),
  1 => 
  array (
    108 => 'J',
    127 => 'K',
    708 => 'P',
    111 => 'X',
  ),
  2 => 
  array (
    78 => 'A',
    125 => 'L',
    127 => 'M',
    178 => 'Q',
  ),
)