2011-10-24 9 views
1

多次元配列をアルファベット順に並べ替えることで、最初の並べ替えが 'building name'、 'last_name'、次に 'first_name '多次元配列を特定のキー値でアルファベット順にソートする方法(PHP)

[70] => 
    Array ( 
    [id] => 635 
    [name] => Mick Kruzic 
    [dob] => 11/05/1968 
    [building_name] => 
    [department] => 
    [phone_ext] => 
    [team_name] => 
    [team_leader] => 
    [party_registered] => 
    [total_points] => 0 
    [total_tickets] => 0 
    [awarded_prizes] => 0 
    [processing_prizes] => 0 
    ) 
+1

私はそこには姓/ firstnameのフィールドを見ない... –

答えて

0

調べたい機能はarray_multisort()です。 http://php.net/manual/en/function.array-multisort.php 多次元配列を並べ替える、または複数の配列を並べ替えることができます。 並べ替えたいキーだけの配列を作成し、その配列を2番目のパラメーターとして使用します。例えばので

あなたは全体の配列は$データだった場合:

foreach($data as $smallarray) $buildingnames[] = $smallarray['building_name']; 
foreach($data as $smallarray) $lastnames[] = $smallarray['last_name']; 
foreach($data as $smallarray) $firstnames[] = $smallarray['first_name']; 
array_multisort($builingnames, ASC, $firstnames, ASC, $lastnames, ASC, $data); 
+0

本当にありがとうございました。それは –

+0

を確実に動作します...私は約1ヶ月前に同様の問題を抱えていました – hackartist

2

usortはあなたの友人です。

function cmp($a, $b){ 
    // compare building, e.g. using strcmp 
    // compare last_name 
    // compare first_name 
    // return 0 ($a == $b), -1 ($a < $b) or 1 ($a > $b) 
} 

usort($array, 'cmp');