Monadoxin Posted August 19, 2009 Share Posted August 19, 2009 Hello, I am having some trouble with sorting an array. The array is populated from a MySQL query, ordering by name. The names of some of the results are groups, so as the array is being created, it checks if the name contains "grp:", and if it does, it expands all of the elements of the group into the array. Below is an example: The current name from the while loop: grp:fruits the name is sent to a function that sends back all of the elements for fruits: expandGroup("grp:fruits"); that sends back this, to add into the array: Apples, Oranges, Grapes, Oranges2 But here is the array before that even happens: America Car Grandfather Then it gets to the grp: results next, but they will not start with grp: when it is expanded. America Car Grandfather Apples Oranges Grapes Oranges2 See, now it is not ordered alphabetically, so then I apply natcasesort to the array: natcasesort($aArray); That does it almost perfectly. Yet it fails when there is a number at the end of something. America Apples Car Grandfather Grapes Oranges2 Oranges Oranges2 is before Oranges. I cannot have that! I have no idea how to order it correctly, I have been trying for hours without success. If anyone has any suggestions I would love to read them. Thanks. Link to comment https://forums.phpfreaks.com/topic/171005-sorting-an-array-with-numbers-at-end/ Share on other sites More sharing options...
Monadoxin Posted August 19, 2009 Author Share Posted August 19, 2009 I did not post enough information last time. Here is the basics of the script: <?php $query = "SELECT" . " name" . " FROM some_table" . " ORDER BY name"; if ($result = mysql_query($query, $dbh)) { $objectArray = array(); while ($rowObject = mysql_fetch_object($result)) { $name = $rowObject->name; $object = new Object(); $object->setByName($name); $memberObjects = array(); if ($object->getDetails() == 'GRP') { replaceGrpWithMembers($object, $memberObjects); $objectArray = array_merge($objectArray, $memberObjects); } else { $sysObjName = ""; getSystemObjectNameForNetworkObjectName($sysObjName, $fullName); $sysObject = new Object(); $sysObject->setByName($sysObjName); $objectArray[$sysObjName] = $sysObject; } } natsort($objectArray); ?> <?php # Functions function replaceGrpWithMembers($object, &$memberObjects) { $parentsArray = $object->getParentsArray(); foreach ($parentsArray as $parentName => $ignoredValue) { $object = new Object(); $object->setByName($parentName); if ($object->getDetails() == 'GRP') { replaceGrpWithMembers($object, $memberObjects); } else { $sysObjName = ""; getSysObjectName($sysObjName, $parentName); $sysObject = new Object(); $sysObject->setByName($sysObjName); $memberObjects[$sysObjName] = $sysObject; } } } ?> It is an array of objects. The array's order is all out of place when it gets the group's members. This is because the group could be called something like "A_Group", and it could be in first place in after the MySQL query. When it is expanded, however, the object names of the children are all different. in some_table: A_Group America Car Grandfather in the other table that the children are in: Oranges Apples Grapes Oranges2 I would get this originally: Apples Oranges Grapes Oranges2 America Car Grandfather But after I used natsort I would get this: America Apples Car Grandfather Grapes Oranges2 Oranges Oranges and Oranges2 are out of place, Oranges2 should be after Oranges. I tried all of the sort functions in PHP, and only natsort worked this well, but it is not perfect. If you have any suggestions, even if you don't think they will work, please post them. Link to comment https://forums.phpfreaks.com/topic/171005-sorting-an-array-with-numbers-at-end/#findComment-901979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.