1

I am trying to sort a multidimensional array based on 1 field so when I run a foreach loop to loop through it will be sorted in the right order...

I have tried several sources but found very few results on multi-dimensional array sorting...

When I run the loop on the below array it shows id field order as 1|2 i want it to be 2|1 what would be the best way to achieve this?

Basically just sort it in descending order...

Array:

array(2) {
    [0]=> array(1) {
        [0]=> array(4) {
            [0]=> string(1) "1" 
            ["id"]=> string(1) "1" 
            [1]=> string(0) "" 
            ["invoicenum"]=> string(0) "" 
        } 
    } 
    [1]=> array(1) { 
        [0]=> array(4) { 
            [0]=> string(1) "2" 
            ["id"]=> string(1) "2" 
            [1]=> string(0) "" 
            ["invoicenum"]=> string(0) "" 
        } 
    } 
}
bansi
  • 55,591
  • 6
  • 41
  • 52
Marcel
  • 874
  • 1
  • 14
  • 28

1 Answers1

0

Have you try this?

array_multisort

<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    $mid[$key]  = $row['id'];
}

// Use $data to sort by the common key
array_multisort($mid, SORT_DESC, $data);
?>
Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51