2014-01-08 242 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 - 所以真实数组中的所有条目都具有“年龄”和“服务”值? –