ntjimb Posted January 13, 2009 Share Posted January 13, 2009 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! Quote Link to comment Share on other sites More sharing options...
dawsba Posted January 13, 2009 Share Posted January 13, 2009 best to do is a asort on the relevant levels looping through a foreach take a look at http://uk3.php.net/asort ie xcuse the shorthand asort($array[cluster][campus]); plus http://uk3.php.net/manual/en/function.array-multisort.php Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 13, 2009 Share Posted January 13, 2009 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"); } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 13, 2009 Share Posted January 13, 2009 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']); Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 13, 2009 Share Posted January 13, 2009 Thanks for that. Although I do see some inconsistencies when sorting anything with leading 0's. But, strcasecmp() does return what I expect to be the results wanted in 95% of cases. Quote Link to comment Share on other sites More sharing options...
ntjimb Posted January 13, 2009 Author Share Posted January 13, 2009 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! Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 13, 2009 Share Posted January 13, 2009 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"); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.