I need help sorting an associative array with PHP.

The sorting should rely on the array key "name", and keep the key/value pairs. Following sorting order: 1) first integer ASC, 2) second integer ASC, 3) third mixed ASC

input

array(6) {
  [0]=>
  array(2) {
    ["name"]=>
    string(13) "60 to 90 in 6"
    ["timing"]=>
    float(4.7)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(15) "40 to 120 in KD"
    ["timing"]=>
    float(3.3)
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(14) "60 to 100 in 4"
    ["timing"]=>
    float(1.5)
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(13) "60 to 90 in 4"
    ["timing"]=>
    float(2.4)
  }
  [4]=>
  array(2) {
    ["name"]=>
    string(15) "140 to 160 in 6"
    ["timing"]=>
    float(2.4)
  }
  [5]=>
  array(2) {
    ["name"]=>
    string(13) "60 to 90 in KD"
    ["timing"]=>
    float(5.7)
  }
}

output order

1, 3, 0, 5, 2, 4

Thank you very much for your help.


Answer:

uasort($acceleration, function($a, $b) {    
    if($a['_sort'][0] == $b['_sort'][0] AND $a['_sort'][2] == $b['_sort'][2])
    {                       
        if($a['_sort'][4] < $b['_sort'][4])
            return -1;
        elseif($a['_sort'][4] > $b['_sort'][4])
            return 1;
        else
            return 0;
    }
    elseif($a['_sort'][0] == $b['_sort'][0])
    {
        if($a['_sort'][2] < $b['_sort'][2])
            return -1;
        elseif($a['_sort'][2] > $b['_sort'][2])
            return 1;
        return 0;
    }
    else
    {
        if($a['_sort'][0] < $b['_sort'][0])
            return -1;
        elseif($a['_sort'][0] > $b['_sort'][0])
            return 1;
        else
            return 0;
    }
});

I'm sure, there is a more elegant way, but may serve as input.

share|improve this question
Where's your code? What have you done so far? We can try to help you with your code, but we can't write it for you. – Uby yesterday

2 Answers

up vote 0 down vote accepted

You need array_keys of this array after sorted with uasort(). The comparison function(closure) should be easy to write.

share|improve this answer
Since you will use string processing for the comparison, you may want to cache the "meaning" of the "name" values if you expect large amount of data: before sorting, insert the to each item the 3 values required for sorting, e.g. for the first, add '_sort'=>array(60,90,'6'), second '_sort'=>array(40,120,'KD')... – Lewyx yesterday
Thx for the inputs. – aebersold yesterday

You should use usort() which allows you to use user-defined comarison function for sorting. So you have to implement function which compares the neme fields and return -1,0 or 1 value depending on the result (less then, equals or greater then). This function may use regex to get three values from name strng field and then compare them using your conditions.

share|improve this answer
Two bad advices in a row. First, usort() does not maintain the keys, which would be the result. Second, don't use regexp; especially not for strings that don't require it; if you're sure of the strings very fixed format, go with explode(). – Lewyx yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.