views:

106

answers:

2

I have an array which is converted from an XML response. What I need to do is sort the array ascending alphabetically using the 'COMPANY' value.

I have attempted to use array_multisort, but I'm having no luck at all. Any help would be greatly appreciated.

Here is the array:

array(1) {
  ["DATASOURCE"]=>
  array(1) {
    ["MEMBER"]=>
    array(4) {
      [0]=>
      array(4) {
        ["REFNO"]=>
        string(6) "000762"
        ["STATUS"]=>
        string(5) "CURRE"
        ["DATE"]=>
        string(8) "20100731"
        ["COMPANY"]=>
        string(80) "Tresham Institute Business Solutions                                            "
      }
      [1]=>
      array(4) {
        ["REFNO"]=>
        string(6) "003721"
        ["STATUS"]=>
        string(5) "CURRE"
        ["DATE"]=>
        string(8) "20100930"
        ["COMPANY"]=>
        string(80) "Triad esign                                                                    "
      }
      [2]=>
      array(4) {
        ["REFNO"]=>
        string(6) "011412"
        ["STATUS"]=>
        string(5) "CURRE"
        ["DATE"]=>
        string(8) "20100630"
        ["COMPANY"]=>
        string(80) "Transpower Drives Ltd                                                           "
      }
      [3]=>
      array(4) {
        ["REFNO"]=>
        string(6) "059647"
        ["STATUS"]=>
        string(5) "CURRE"
        ["DATE"]=>
        string(8) "20100630"
        ["COMPANY"]=>
        string(80) "Trek-Kits Ltd                                                                   "
      }
    }
  }
}
A: 

use usort() function

Karthik
Please refer this posthttp://stackoverflow.com/questions/2672900/sort-an-array-by-a-child-arrays-value/2672925#2672925
Karthik
+3  A: 

You'll need a function that takes the two items to be sorted, and compares them.

function sort_by_company($a, $b)
{
    return strcmp($a['COMPANY'], $b['COMPANY']);
}

Then, use the usort function.

usort($arr['DATASOURCE']['MEMBER'], 'sort_by_company');
cHao
Brilliant, thanks for your help, it works perfectly.
meteoracle