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
https://forums.phpfreaks.com/topic/140690-another-multidimensional-sort-question/
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");
}

mjdamato, just fyi: all of this:

 

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

 

Could be replaced with:

 

return strcasecmp($a['type'],$b['type']);

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!

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");
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.