this worked for me.. might be useful to someone... // sort the array.. $current = 0; // we begin at the top $totals = forum_count_values($r);
while ( $current < sizeof($r)) { $c = $r[$current][FORUM_POST_FIELD_ID]; // count the number of posts that are replies to current. $total = $totals[$c]; $j=0; $i=$current + 1; // we start after current.. while (true) { $id = $r[$i][FORUM_POST_FIELD_ID]; $p = $r[$i][FORUM_POST_FIELD_PARENT]; // set parent. if ($p != $c) { // if parent not equal to current move to last array_push($r, $r[$i]); // add a copy of element last array_splice($r,$i,1); // remove the old element } else { $j++; // if current and parent are equal move one step closer to total $i++; } if ($j==$total) break; } $current++; // move to next position } // end while "Leif HöGberg" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > I have an array where each element contains an array that in turn contains > 2 numbers indexed by "id" and "parent". > > Every id except for the first one belongs to another, its parent. > > What I want to do now is sort the array so that every id is put after its > parent starting with id 1. > > Example: > arr1 = ( > 0 => array("id" => 1, "parent" => 0), > 1 => array("id" => 2, "parent" => 1), > 2 => array("id" => 3, "parent" => 1), > 3 => array("id" => 4, "parent" => 2), > 4 => array("id" => 5, "parent" => 3), > 5 => array("id" => 6, "parent" => 2), > 6 => array("id" => 7, "parent" => 1), > 7 => array("id" => 8, "parent" => 4), > 8 => array("id" => 9, "parent" => 5) > ) > > In other words they are in the following order.. > 1=0 > 2=1 > 3=1 > 4=2 > 5=3 > 6=2 > 7=1 > 8=4 > 9=5 > > After the sort I want them to be in the following order.. > 1=0 > 2=1 > 4=2 > 8=4 > 6=2 > 3=1 > 5=3 > 9=5 > 7=1 > > Note that id 6 isn't placed right after id 4 since id 4 has parents of its > own which have precedence. > > An easier way to illustrate this would be by indenting the list.. > > > > 1=0 > 2=1 > 4=2 > 8=4 > 6=2 > 3=1 > 5=3 > 9=5 > 7=1 > > > The index of $arr1 makes no difference so the array can be shifted or > rebuilt.. > > If you are wondering I need this for a forum that im building. If you have > any suggestions as how to do this better feel free to tell me all about it > :) > > > > Thanks in advance. > > > > // Leif Högberg > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php