Jump to content

Another multidimensional sort question


ntjimb

Recommended Posts

I've got a tricky situation here (I think).

I've got an array created from an xml file...

Array
(
    [cluster] => Array
        (
            [campus] => Array
                (
                    [0] => Array
                        (
                            [name] => PlanoSenior
                            [item] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => PL001PSHS01
                                            [type] => server
                                            [mgmt] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => mgmt1
                                                            [url] => http://google.com
                                                        )

                                                    [1] => Array
                                                        (
                                                            [name] => mgmt2
                                                            [url] => http://google.com
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [name] => APCdevice
                                            [type] => UPS
                                            [mgmt] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => mgmt1
                                                            [url] => http://google.com
                                                        )

                                                    [1] => Array
                                                        (
                                                            [name] => mgmt2
                                                            [url] => http://google.com
                                                        )

                                                )

                                        )

                                    [2] => Array
                                        (
                                            [name] => PL001PSHSIN1
                                            [type] => server
                                            [mgmt] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => mgmt1
                                                            [url] => http://google.com
                                                        )

                                                    [1] => Array
                                                        (
                                                            [name] => mgmt2
                                                            [url] => http://google.com
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

I left out the other campuses for legibility, but essentially the cluster->campus->0 will increment by 1 for each campus.

 

Now what I need to do is sort the entire array (all campuses) by the campus name (ie. PlanoSenior) AND sort the result so that the item types are grouped together (server items together).

 

Now I could also create an array for just the campus I'm looking at (url?campus=PlanoSenior) and then sort the item types, but I'm not sure how to interact with the dynamic value when I have multiple xml tags of the same name (<campus></campus>, ie...cluster->campus->0).

 

I hope I'm clear enough. I'm a beginner php'er, but no snippets or examples I've found seem to apply.

 

THANKS!

Link to comment
Share on other sites

Assuming $campusarray is the name of the array in question, this will do what you want:

 

function sortNames($a, $b)
{
    $strA = strtoupper($a['name']);
    $strB = strtoupper($b['name']);

    if ($strA == $strB) { return 0; }
    return ($strA < $strB) ? -1 : 1;
}

function sortTypes($a, $b)
{
    $strA = strtoupper($a['type']);
    $strB = strtoupper($b['type']);
    if ($strA == $strB) { return 0; }
    return ($strA < $strB) ? -1 : 1;
}

//Sort the array by campus names
//Use 'usort' if you do not need/want to maintain index associattions
uasort($campusarray['cluster']['campus'], "sortNames");

//Sort the types for each campus element
//Use 'usort' if you do not need/want to maintain index associattions
foreach ($campusarray['cluster']['campus'] as $campusID => $campusAry)
{
    uasort($campusarray['cluster']['campus'][$campusID]['item'], "sortTypes");
}

Link to comment
Share on other sites

wow! perfect! I would have expected a lot more code for that. Ok so this is what I have down...

 

<?php
function sortNames($a, $b)
{
    return strcasecmp($a['name'],$b['name']);
}

function sortTypes($a, $b)
{
    return strcasecmp($a['type'],$b['type']);
}

//Sort the array by campus names
//Use 'usort' if you do not need/want to maintain index associations
usort($arrayData['cluster']['campus'], "sortNames");
echo '<pre>';
print_r($arrayData);
echo '</pre>';

//Sort the types for each campus element
//Use 'usort' if you do not need/want to maintain index associattions
foreach ($arrayData['cluster']['campus'] as $campusID => $campusAry)
{
    usort($arrayData['cluster']['campus'][$campusID]['item'], "sortTypes");
}
echo '<pre>';
print_r($arrayData);
echo '</pre>';
?>

 

The first print_r sorts the array by campus name. Nice.

 

Then the second sorts the items within, which is perfect!

 

I'm curious why (I have php errors enabled) it displays

Warning: usort(): The argument should be an array in /srv/www/htdocs/dev/tss/test2.php on line 76 Warning: usort(): The argument should be an array in /srv/www/htdocs/dev/tss/test2.php on line 76

out to the browser the line before the last print_r?

 

Other than this warning, it works just great! Thanks for the quick responses!

Link to comment
Share on other sites

I would guess that within the data there might be two records that do not have a sub-element for "['cluster']['campus'][$campusID]['item']" or that element is not an array. Try changing that code to this:

 

//Sort the types for each campus element
//Use 'usort' if you do not need/want to maintain index associattions
foreach ($arrayData['cluster']['campus'] as $campusID => $campusAry)
{
    if (is_array($arrayData['cluster']['campus'][$campusID]['item']))
    {
        usort($arrayData['cluster']['campus'][$campusID]['item'], "sortTypes");
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.