2014-01-08 429 views
0

上multisort考慮以下關聯數組:PHP - 關聯數組

$arrEmployees['marco polo'] = array(age => 40, service => 5); 
$arrEmployees['jane austen'] = array(age => 30, service => 9); 
$arrEmployees['carl marx'] = array(age => 30, service => 7); 

怎麼用在array_multisort按年齡遞減和服務ASC訂購?例3中php.net似乎只與數字索引工作...

回答

1
$arrEmployees['marco polo'] = array(age => 40, service => 5); 
$arrEmployees['jane austen'] = array(age => 30, service => 9); 
$arrEmployees['carl marx'] = array(age => 30, service => 7); 

foreach ($arrEmployees as $key => $row) { 
    $age[$key] = $row['age']; 
    $service[$key] = $row['service']; 
} 

array_multisort($age, SORT_DESC, $service, SORT_ASC, $arrEmployees); 

var_dump($arrEmployees); 

array(3) { 
    ["marco polo"]=> 
    array(2) { 
    ["age"]=> 
    int(40) 
    ["service"]=> 
    int(5) 
    } 
    ["carl marx"]=> 
    array(2) { 
    ["age"]=> 
    int(30) 
    ["service"]=> 
    int(7) 
    } 
    ["jane austen"]=> 
    array(2) { 
    ["age"]=> 
    int(30) 
    ["service"]=> 
    int(9) 
    } 
} 

這是正確的,並且所有關聯指標仍然完好​​無損

+0

我得到一個「數組大小不一致「警告,並且數組仍然是相同的 – user3174311

+0

不完全按照我已經顯示的那樣運行代碼,使用您提供的數組 - http://3v4l.org/SLa2E#tabs - 所以真實數組中的所有條目都具有「年齡」和「服務」值? –