Wednesday, 29 August 2018

PHP - Sorting a multidimensional array

Possible Duplicate:
Sorting an associative array in PHP
Sorting a multidimensional array in PHP?
I having some difficulties with sorting an array... I've got an array with (sub)categories and items, but I don't manage to sort them...
The array typically looks like this:
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

To make things clear,
the categories are: Professional Services ApplicationsBusiness Applications and acADDemICT.
the subcategories are: SpecializedTechnologiesJAVA and Microsoft (some don't have a subcategory!)
I loop through this array with this PHP code:
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>';
        }
    }
}

I've tried all different kinds of sort function (sort()asort()ksort(),...), but I didn't end up with the correct result.
I'm aiming for the a alphabetically order of categories, subcategories and items...
So my ideal result would look like this:
**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

UPDATE:
Here is my 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 ) ) ) )


You should write a recursive function that walks through each array and if the element is an array in itself, sorts it, and then sorts the original array.
Pseudocode:
function mysort($array) {
  foreach ($array as $key=>$value) {
    if (is_array($value)) $array[$key] = mysort($value);
  }
  sort($array);
  return $array;
}

0 comments:

Post a Comment