0

I have this array of arrays,

Array
(
[0] => Array
    (
        [id] => 1
        [title] => AP-2 (1)
    )

[1] => Array
    (
        [id] => 2
        [title] => AC-1 (2)
    )

[2] => Array
    (
        [id] => 3
        [title] => AB-3 (1)
    )

[3] => Array
    (
        [id] => 4
        [title] => AD-2 (3)
    )

[4] => Array
    (
        [id] => 5
        [title] => AE-2 (1)
    )

),

and I need to sort it in a way in which it will look like this,

Array
(
[0] => Array
    (
        [id] => 1
        [title] => AB-3 (1)
    )

[1] => Array
    (
        [id] => 2
        [title] => AC-1 (2)
    )

[2] => Array
    (
        [id] => 3
        [title] => AD-2 (3)
    )

[3] => Array
    (
        [id] => 4
        [title] => AE-2 (1)
    )

[4] => Array
    (
        [id] => 5
        [title] => AP-2 (1)
    )

)

What happened here is basically, sort the arrays using the title key alphabetically or maybe sort it using natsort() or natcasesort(). How would I actually do the sorting? Thanks in advance.

Tsukimoto Mitsumasa
  • 541
  • 4
  • 19
  • 42
  • Implement your own swap(ele1, ele2) function and then implement a sorting algorithm of your choice. – Tro Jul 12 '13 at 15:31

1 Answers1

2
function sorter($key){
    return function ($a, $b) use ($key) {
        return strcmp($a[$key], $b[$key]);
    };
}
usort($arr, sorter('title'));
var_dump($arr);

For versions of PHP prior to 5.3, use:

function sorter($arr, $index) {
    foreach($arr as $key => $value) {
        $arr2[$key] = strtolower($value[$index]);
    }
    asort($arr2);
    foreach($arr2 as $key =>$value) {
        $arr3[] = $arr[$key];
    }
    return $arr3;
}
var_dump(sorter($arr, 'title'));
Expedito
  • 7,771
  • 5
  • 30
  • 43
  • @CHiRiLo - I ran the code, and it didn't give me any syntax error. The output is shown above. – Expedito Jul 12 '13 at 16:12
  • @CHiRiLo - `$a` and `$b` represent the arrays with numerical keys such as `[0] => array('id' => 3, '$title' => 'AB-3')`. On each comparison `$a` and `$b` will represent different arrays, comparing the string value of "title." – Expedito Jul 12 '13 at 16:15
  • Am I doing this the right way, I assigned the first array above to a variable called `$arr`, then in your solution, I only replaced `$row_array` with `$arr`. Was that the right way to do it? – Tsukimoto Mitsumasa Jul 12 '13 at 16:21
  • 2
    It only works 5.3 and above due to closure usage. – Ja͢ck Jul 12 '13 at 16:56
  • I changed the version of my php to 5.3 and then it magically worked. Whew. Thanks @Jack and also thank you Pé de Leão. – Tsukimoto Mitsumasa Jul 12 '13 at 17:38
  • ahm, @Jack and Pé de Leão, is there another way in which I can make this work using php versions lower than 5.3? The case is that, I can't readily change the php version the web server my client is using so my alternative is that I will find a solution which works on at least version 5.2.17. I badly need your help. Thanks. – Tsukimoto Mitsumasa Jul 12 '13 at 19:18
  • I tried your edit, but it won't work. Btw, the php version the web server the client is using is 5.2.17. That is why I am asking for a solution which will work on version 5.2.17. – Tsukimoto Mitsumasa Jul 12 '13 at 19:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33386/discussion-between-chi-rilo-and-pe-de-leao) – Tsukimoto Mitsumasa Jul 12 '13 at 19:58
  • Hello Pé de Leão, I modified your solution a bit. I almost forgot that I am supposed to use `natsort()` instead. So I replaced `asort()` with `natsort()` and it perfectly worked. Merely, upvoting your answer does not suffice of how much thankful I am but thanks anyway. :) – Tsukimoto Mitsumasa Jul 12 '13 at 20:47