0

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 Applications, Business Applications and acADDemICT.
the subcategories are: Specialized, Technologies, JAVA 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 ) ) ) )
Community
  • 1
  • 1
Michiel
  • 7,855
  • 16
  • 61
  • 113
  • Post a `print_r` of your array, then somebody can take that and work on a solution. – web-nomad Apr 11 '12 at 10:16
  • The first code-block you see in my question is a `var_dump` of my array. Shouldn't that suffice? – Michiel Apr 11 '12 at 10:22
  • i want your array only...not all information about each and every element of that array, like type,length etc. That i can pick and work towards a solution. Rather than a `var_dump`, i ask `print_r` – web-nomad Apr 11 '12 at 10:30
  • Ok, here you go (I didn't knew how to split the lines, so I hope you don't mind the format)... – Michiel Apr 11 '12 at 10:39

2 Answers2

1

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;
}
Cray
  • 2,396
  • 19
  • 29
  • After some checking, you might want to check out the function array_multisort() http://se.php.net/manual/en/function.array-multisort.php – Cray Apr 11 '12 at 10:04
1

As you have three levels of keys to be sorted, you somewhat have to apply ksort to all 2d-levels of the multi-dimensional array:

$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);

Output:

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
                        )

                )

        )

)
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks for the effort, but as far as I can see, this function isn't doing exactly what I was aiming for... It's dropping some values and I'm not sure what the `function` statement is doing in the first `array_map`. – Michiel Apr 11 '12 at 10:53
  • @Michiel: Indeed, there was a minor issue with the function. I've also removed the function statement for `array_map` because if you have different levels of sub-categories, the first example won't work anyway. – hakre Apr 11 '12 at 11:34