0

可能重复:在 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
子类别是:SpecializedTechnologiesJAVAMicrosoft (有些没有子类别!)

我用这个 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 ) ) ) )
4

2 回答 2

1

您应该编写一个遍历每个数组的递归函数,如果元素本身是一个数组,则对其进行排序,然后对原始数组进行排序。

伪代码:

function mysort($array) {
  foreach ($array as $key=>$value) {
    if (is_array($value)) $array[$key] = mysort($value);
  }
  sort($array);
  return $array;
}
于 2012-04-11T09:59:44.883 回答
1

由于要对三个级别的键进行排序,因此您必须在某种程度上应用ksort到多维数组的所有 2d 级别:

$array = array(
    'Category Z' => array(
        'Category Z - Z' => array(
            'Category Z - Z - Z' => array('2701'),
            'Category Z - Z - X' => array('2601'),
        )
    ),
    'Category A' => array(
        'Category A - A' => array(
            'Category A - A - A' => array('2200'),
        )
    ),
);

$ksort = function($array) use (&$ksort) {
    if (!is_array($array)) return $array;
    ksort($array);
    return array_map($ksort, $array);
};

$array = $ksort($array);

print_r($array);

输出:

Array
(
    [Category A] => Array
        (
            [Category A - A] => Array
                (
                    [Category A - A - A] => Array
                        (
                            [0] => 2200
                        )

                )

        )

    [Category Z] => Array
        (
            [Category Z - Z] => Array
                (
                    [Category Z - Z - X] => Array
                        (
                            [0] => 2601
                        )

                    [Category Z - Z - Z] => Array
                        (
                            [0] => 2701
                        )

                )

        )

)
于 2012-04-11T10:04:26.497 回答