Laurence Tuck
Laurence Tuck

Reputation: 420

php array sorting and grouping

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: Sort Multi-dimensional 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.

Upvotes: 0

Views: 2052

Answers (3)

Laurence Tuck
Laurence Tuck

Reputation: 420

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.

Upvotes: 0

apfelbox
apfelbox

Reputation: 2634

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.

Upvotes: 0

arkascha
arkascha

Reputation: 42915

Take a look at the uasort() function:

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

Upvotes: 1

Related Questions