Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array of Towns that have no sorting whatsoever. I would like to sort by the [category][3] which is the province and then the [category][0] which is the region and display each Grouped Province with its regions and then towns underneath it. So the following array:

Array
(
    [0] => Array
        (
            [name] => Name One
            [id] => 1
            [link] => http://mylink1.com
            [category] => Array
                (
                    [0] => Region 1
                    [1] => Town 7
                    [2] => Country
                    [3] => Province 2
                )    
        )

    [1] => Array
        (
            [name] => Name Two
            [id] => 2
            [link] => http://mylink2.com
            [category] => Array
                (
                    [0] => Region 1
                    [1] => Town
                    [2] => Country
                    [3] => Province 3
                )    
        )

    [2] => Array
        (
            [[name] => Name Three
            [id] => 3
            [link] => http://mylink3.com
            [category] => Array
                (
                    [0] => Region 1
                    [1] => Town 5
                    [2] => Country
                    [3] => Province 2
                )
        )

    [6] => Array
        (
            [name] => Name Four
            [id] => 4
            [link] => http://mylink4.com
            [category] => Array
                (
                    [0] => Region 1
                    [1] => Town 1
                    [2] => Country
                    [3] => Province 1
                )
        )

)

... should end up looking like this:

Country (all the same)

Province 1
- Region 1
- - Town 1 name, id, link

Province 2
- Region 1
- - Town 5 name, id, link
- - Town 7 name, id, link

Province 3
- Region 1
- - Town 1 name, id, link

Province is the Primary Grouping factor, then sorted by Region, the Towns in no particular order but I guess Alphabetically would make sense.

I have managed to sort the array by Category using this reference: PHP sort multidimensional array by value but cannot fathom how to sort any further without referencing the Province specifically in a loop by using its name. i.e.

/// shortened version..
foreach($array as $key=>$value)...
   if($value == "Province 1") : do something here with these matches
     ... etc

Any help would be appreciated.

share|improve this question
1  
write a comparison function and use it with usort() php.net/manual/en/function.usort.php – alfasin Aug 1 '12 at 22:34

3 Answers

Take a look at the uasort() function:

http://www.php.net/manual/en/function.uasort.php

share|improve this answer
I'd go with usort(), unless the keys in the array (0,1,2,6) were intended to show they keys are important :) - difficult to tell! But yes, a custom comparison function is the way to go. – halfer Aug 3 '12 at 9:52

I don't think that you can do this in one step.

You can group your values like this:

$grouped = array();

foreach ($data as $group)
{
    $grouped[ $group['category'][3] ][ $group['category'][0] ][ $group['category'][1] ] = array(/* ... */);
}

But you will still have to sort every array (and it's sub-arrays) using ksort().

You should check, whether you can get the values already presorted. If you, for example, are using a database to retrieve this values, it would be quite trivial to sort them in the database and bring them in the correct (grouped) structure in your PHP code.

share|improve this answer
...not exactly what I have resorted to, but definitely the closest - this cannot be done in one step and using any sort function then becomes useless. the grouping is done by iterating through the array and then the result of those groups output. – Innate Aug 3 '12 at 9:37

looping through the array I've used a switch for the category I'm looking for and then built another array for each group. from there I can sort by region:

foreach($arr as $town){                             
    switch($town['blogcats']){                                  
        case "Province 1" : $province1[] = $town;
        break;
        case...
        etc
    }
}

Then each new grouped array can be output:

foreach($province1 as $eachtown) {
    echo $newtown['name'];
    ... and so forth.
}

Within the second loop sorting could be done on the Region. Each Province is manually referenced in a list on my page which also gives me control over placement.

Howver, this answer could also be improved... but the above works without too much effort.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.