-2

I've got an array as follows:

Array (
[id] => Array
    (
        [0] => 3321
        [1] => 3318
        [2] => 3320
        [3] => 3319
        [4] => 3324
        [5] => 3322
    )

[name] => Array
    (
        [0] => Carla Taku
        [1] => Honey-Pearl Te Moni
        [2] => Monique Koroua
        [3] => Summer Hellier
        [4] => Wayne Kahukiwa
        [5] => Natasha Merito
    )

[courses] => Array
    (
        [0] => 2
        [1] => 1
        [2] => 1
        [3] => 1
        [4] => 1
        [5] => 1
    )

)

I want to sort this by "name", but I can't work out how to do this. I can't make it multi-dimensional due to some other code. I've tried usort, ksort, array_sort, array_multisort, but I'm not experienced enough to sort this.

halfer
  • 19,824
  • 17
  • 99
  • 186
dpDesignz
  • 1,909
  • 10
  • 34
  • 70

3 Answers3

4

I think array_multisort is what you are after:

array_multisort($array['name'], $array['id'], $array['courses']);
Anthony
  • 36,459
  • 25
  • 97
  • 163
1

I just came across sorting stuff yesterday. Hopefully this could give you some hint.

function SortByName($a,$b){
    return strcasecmp($a['name'], $b['name']);
}

usort($YourArray, 'SortByName');
UserProg
  • 629
  • 1
  • 8
  • 22
  • I have tried this, but I keep getting the error `Undefined index: name`? But if I echo the array after the function it's changed? – dpDesignz Jun 12 '14 at 03:57
1

Sorry for not using your specific array, I got lazy. However this should work for you, easy as pie.

$ar = array(
    array("10", 11, 100, 100, "a"),
    array(1, 2, "2", 3, 1)
);

$temp = $ar[1];

$count = 0;

asort($ar[0]);
foreach ($ar[0] as $key => $value) {
    $ar[1][$count] = $temp[$key];
    $count++;
}

print_r($ar);
halfer
  • 19,824
  • 17
  • 99
  • 186
Charles
  • 427
  • 3
  • 6