0

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                                                                   "
      }
    }
  }
}

2 Answers 2

6

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');
Sign up to request clarification or add additional context in comments.

Comments

0

You can use usort() PHP function.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.