可能重复:在 PHP 中对
关联数组进行
排序 在 PHP 中对多维数组进行排序?
我在对数组进行排序时遇到了一些困难......我有一个包含(子)类别和项目的数组,但我无法对它们进行排序......
该数组通常如下所示:
array
'Professional Services Applications' =>
array
'Specialized Technologies' =>
array
'Test Michiel, part 2' =>
array
0 => string '2071' (length=4)
'JAVA' =>
array
'Senior Apple Engineer' =>
array
0 => string '2067' (length=4)
'Junior Apple Engineer' =>
array
0 => string '2069' (length=4)
'Microsoft' =>
array
'Senior ECM Consultant' =>
array
0 => string '2059' (length=4)
'Junior ECM Consultant' =>
array
0 => string '2082' (length=4)
'Business Applications' =>
array
'' =>
array
'Business Analyst' =>
array
0 => string '2053' (length=4)
'Test business dingen ' =>
array
0 => string '2062' (length=4)
'acADDemICT' =>
array
'' =>
array
'Test Michiel
为了清楚起见,
这些类别是:专业服务应用程序、业务应用程序和acADDemICT。
子类别是:Specialized、Technologies、JAVA和Microsoft
(有些没有子类别!)
我用这个 PHP 代码遍历这个数组:
foreach ($job_list as $category => $subcategory) {
$page .= '<h2>' . $category .'</h2>';
foreach ($subcategory as $name => $values) {
if (!empty($name)) {
$page .= '<h3>' . $name . '</h3>';
}
foreach ($values as $vacancy => $link) {
$page .= '<li>' . l("$vacancy", "node/$link[0]") . '</li>';
}
}
}
我尝试了所有不同类型的排序函数(sort()
, asort()
, ksort()
,...),但我没有得到正确的结果。
我的目标是按字母顺序排列类别、子类别和项目...
所以我的理想结果是这样的:
**aCCademICT:**
- Test Michiel
**Business Applications:**
- Business Analyst
- Test business
**Professional Services Applications:**
*JAVA*
- Junior Apple Engineer
- Senior Apple Enineer
*Microsoft*
- Junior ECM Consultant
- Senior ECM Consultant
*Specialize Technologies*
- Test Michiel, part 2
更新:
这是我的print_r
:
Array ([Professional Services Applications] => Array ( [Specialized Technologies] => Array ( [Test van Michiel, part 2 ] => Array ( [0] => 2071 ) ) [JAVA] => Array ( [Senior Apple Engineer ] => Array ( [0] => 2067 ) ) [Microsoft] => Array ( [Junior ECM Consultant ] => Array ( [0] => 2059 ) [Senior ECM Consultant] => Array ( [0] => 2087 )) ) [acADDemICT] => Array ( [] => Array ( [Test van Michiel ] => Array ( [0] => 2063 ) ) ) [Business Applications] => Array ( [] => Array ( [Business Analyst ] => Array ( [0] => 2053 ) [Test business dingen ] => Array ( [0] => 2062 ) ) ) )