Well, it might not be the best way, but I been trying to do it in a different way without success, that is why I started doing it like this, my problem is that to start with I have an array which I have to sort based on different conditions and I tried using usort without success, for example if I have:
Array (
[0] => stdClass Object ( [id] => 238 [parent_id] => 1 [level] => 1 [ordering] => 0 )
[1] => stdClass Object ( [id] => 445 [parent_id] => 238 [level] => 2 [ordering] => 0 )
[2] => stdClass Object ( [id] => 621 [parent_id] => 1 [level] => 1 [ordering] => -5 )
[3] => stdClass Object ( [id] => 250 [parent_id] => 445 [level] => 2 [ordering] => 0 )
[4] => stdClass Object ( [id] => 223 [parent_id] => 445 [level] => 2 [ordering] => 0 )
[5] => stdClass Object ( [id] => 367 [parent_id] => 1 [level] => 1 [ordering] => -3 )
[6] => stdClass Object ( [id] => 7 [parent_id] => 1 [level] => 1 [ordering] => -1 )
[7] => stdClass Object ( [id] => 502 [parent_id] => 394 [level] => 2 [ordering] => 0 )
[8] => stdClass Object ( [id] => 356 [parent_id] => 1 [level] => 1 [ordering] => -2 )
[9] => stdClass Object ( [id] => 908 [parent_id] => 238 [level] => 2 [ordering] => 0 )
[10] => stdClass Object ( [id] => 394 [parent_id] => 1 [level] => 1 [ordering] => -4 )
[11] => stdClass Object ( [id] => 1010 [parent_id] => 238 [level] => 2 [ordering] => -1 )
[12] => stdClass Object ( [id] => 610 [parent_id] => 1 [level] => 1 [ordering] => 0 )
)
That array I have to first sort the items by the array->ordering as long as it is different than 0 and if it is then I have to sort them by array->id also if the item happens to have a parent id other than 1 then I have to order those items in the same way as explained above but just bellow the item that matches their parent id with the id, basically that above array should end up like this:
Array (
[0] => stdClass Object ( [id] => 621 [parent_id] => 1 [level] => 1 [ordering] => -5 )
[1] => stdClass Object ( [id] => 394 [parent_id] => 1 [level] => 1 [ordering] => -4 )
[2] => stdClass Object ( [id] => 502 [parent_id] => 394 [level] => 2 [ordering] => 0 )
[3] => stdClass Object ( [id] => 367 [parent_id] => 1 [level] => 1 [ordering] => -3 )
[4] => stdClass Object ( [id] => 356 [parent_id] => 1 [level] => 1 [ordering] => -2 )
[5] => stdClass Object ( [id] => 7 [parent_id] => 1 [level] => 1 [ordering] => -1 )
[6] => stdClass Object ( [id] => 238 [parent_id] => 1 [level] => 1 [ordering] => 0 )
[7] => stdClass Object ( [id] => 1010 [parent_id] => 238 [level] => 2 [ordering] => -1 )
[8] => stdClass Object ( [id] => 445 [parent_id] => 238 [level] => 2 [ordering] => 0 )
[9] => stdClass Object ( [id] => 908 [parent_id] => 238 [level] => 2 [ordering] => 0 )
[10] => stdClass Object ( [id] => 610 [parent_id] => 1 [level] => 1 [ordering] => 0 )
[11] => stdClass Object ( [id] => 223 [parent_id] => 445 [level] => 2 [ordering] => 0 )
[12] => stdClass Object ( [id] => 250 [parent_id] => 445 [level] => 2 [ordering] => 0 )
)
right now I am done with the work, although it might not necessarily be the best way, bellow is the code and I am open to any suggestions.
function order_by_ordering($a, $b){
if ($a->ordering == $b->ordering){
return 0;
}
return ($a->ordering < $b->ordering) ? -1 : 1;
}
function order_by_id($a, $b){
if ($a->id == $b->id){
return 0;
}
return ($a->id < $b->id) ? -1 : 1;
}
function build_array($array_1, $array_2, $final_array){
foreach ($array_1 as &$value){
$final_array[] = $value;
}
foreach ($array_2 as &$value){
$final_array[] = $value;
}
return $final_array;
}
$menu_items = $db->loadObjectList();//this is the array to be processed
$item_count = count($menu_items);
$root_items = array();
$root_items_ = array();
$child_items_2 = array();
$child_items_2_ = array();
$child_items_3 = array();
$child_items_3_ = array();
$child_items_4 = array();
$child_items_4_ = array();
$child_items_5 = array();
$child_items_5_ = array();
foreach($menu_items as &$value){
if ($value->level == '1' && $value->ordering != '0'){
$root_items[] = $value;
}
else if ($value->level == '1' && $value->ordering == '0'){
$root_items_[] = $value;
}
else if ($value->level == '2' && $value->ordering != '0'){
$child_items_2[] = $value;
}
else if ($value->level == '2' && $value->ordering == '0'){
$child_items_2_[] = $value;
}
else if ($value->level == '3' && $value->ordering != '0'){
$child_items_3[] = $value;
}
else if ($value->level == '3' && $value->ordering == '0'){
$child_items_3_[] = $value;
}
else if ($value->level == '4' && $value->ordering != '0'){
$child_items_4[] = $value;
}
else if ($value->level == '4' && $value->ordering == '0'){
$child_items_4_[] = $value;
}
else if ($value->level == '5' && $value->ordering != '0'){
$child_items_5[] = $value;
}
else if ($value->level == '5' && $value->ordering == '0'){
$child_items_5_[] = $value;
}
else {}
}
usort($root_items, 'order_by_ordering');
usort($root_items_, 'order_by_id');
usort($child_items_2, 'order_by_ordering');
usort($child_items_2_, 'order_by_id');
usort($child_items_3, 'order_by_ordering');
usort($child_items_3_, 'order_by_id');
usort($child_items_4, 'order_by_ordering');
usort($child_items_4_, 'order_by_id');
usort($child_items_5, 'order_by_ordering');
usort($child_items_5_, 'order_by_id');
$root_array = array();
$child_array_2 = array();
$child_array_3 = array();
$child_array_4 = array();
$child_array_5 = array();
$root_array = build_array($root_items, $root_items_, $root_array);
$child_array_2 = build_array($child_items_2, $child_items_2_, $child_array_2);
$child_array_3 = build_array($child_items_3, $child_items_3_, $child_array_3);
$child_array_4 = build_array($child_items_4, $child_items_4_, $child_array_4);
$child_array_5 = build_array($child_items_5, $child_items_5_, $child_array_5);
/*
Start building the menu
*/
echo '<ul id="tlak_menu" >';
foreach ($root_array as &$value_1){
$id_1 = $value_1->id;
$has_child_2 = false;
foreach($child_array_2 as &$value_2){
if($value_2->parent_id == $id_1){
$has_child_2 = true;
}
}
if ($has_child_2 == true){
echo '<li class="tlak_parent_1" ><a href="' . $doc_root . $value_1->link . '" >' . $value_1->title . '</a><ul>';
foreach($child_array_2 as &$value_2){
if($value_2->parent_id == $id_1){
$id_2 = $value_2->id;
$has_child_3 = false;
foreach($child_array_3 as &$value_3){
if($value_3->parent_id == $id_2){
$has_child_3 = true;
}
}
if ($has_child_3 == true){
echo '<li class="tlak_parent_2" ><a href="' . $doc_root . $value_2->link . '" >' . $value_2->title . '</a><ul>';
foreach($child_array_3 as &$value_3){
if($value_3->parent_id == $id_2){
$id_3 = $value_3->id;
$has_child_4 = false;
foreach($child_array_4 as &$value_4){
if($value_4->parent_id == $id_3){
$has_child_4 = true;
}
}
if ($has_child_4 == true){
echo '<li class="tlak_parent_3" ><a href="' . $doc_root . $value_3->link . '" >' . $value_3->title . '</a><ul>';
foreach($child_array_4 as &$value_4){
if($value_4->parent_id == $id_3){
$id_4 = $value_4->id;
$has_child_5 = false;
foreach($child_array_5 as &$value_5){
if($value_5->parent_id == $id_4){
$has_child_5 = true;
}
}
if ($has_child_5 == true){
echo '<li class="tlak_parent_4" ><a href="' . $doc_root . $value_4->link . '" >' . $value_4->title . '</a><ul>';
foreach($child_array_5 as &$value_5){
if($value_5->parent_id == $id_4){
echo '<li><a href="' . $doc_root . $value_5->link . '" >' . $value_5->title . '</a></li>';
}
}
echo '</ul></li>';
}
else {
echo '<li ><a href="' . $doc_root . $value_4->link . '" >' . $value_4->title . '</a></li>';
}
}
}
echo '</ul></li>';
}
else {
echo '<li ><a href="' . $doc_root . $value_3->link . '" >' . $value_3->title . '</a></li>';
}
}
}
echo '</ul></li>';
}
else {
echo '<li ><a href="' . $doc_root . $value_2->link . '" >' . $value_2->title . '</a></li>';
}
}
}
echo '</ul></li>';
}
else {
echo '<li ><a href="' . $doc_root . $value_1->link . '" >' . $value_1->title . '</a></li>';
}
}