-1

i feel stupid right now i cant get my head around it

i got a array with stdClass Objects and i whant sort them new or build a other array with it

Array
(
    [0] => stdClass Object
        (
            [id] => 15
            [top_id] => 13
            [title] => Menu 6
            [online] => 1
            [displayOrder] => 15
        )

    [1] => stdClass Object
        (
            [id] => 14
            [top_id] => 13
            [title] => Menu 5
            [online] => 0
            [displayOrder] => 14
        )

    [2] => stdClass Object
        (
            [id] => 13
            [top_id] => 0
            [title] => Menu 4
            [online] => 0
            [displayOrder] => 13

        )

    [3] => stdClass Object
        (
            [id] => 12
            [top_id] => 11
            [title] => Menu 3
            [online] => 1
            [displayOrder] => 12
        )

)

The Array i whant is like

(
    [2] => stdClass Object
        (
            [id] => 13
            [top_id] => 0
            [title] => Menu 4
            [online] => 0
            [displayOrder] => 13
            [children] =>
            (
               [id] => 11
               [top_id] => 13
               [title] => Menu 2
               [online] => 0
               [displayOrder] => 11
               [children] =>
                (
                    [id] => 12
                    [top_id] => 11
                    [title] => Menu 3
                    [online] => 1
                    [displayOrder] => 12
                )
)

I tryed today multiple thing i found online but i cant get my head around the right answer/ or get it to work

pls could show me someone in the right direction with a small explaination why thats the way ?

thank you and best regards

Limuz
  • 5
  • 3
  • What have you already tried so far? If you show us the code you tried that isn't working, and tell us details of what the problem is that you are having, we can take a look and help you get it working. – FluffyKitten Aug 17 '20 at 13:32
  • my first go whas i tryed to foreach the array and start of with adding to a new array every obj with top_id= 0 and unset the used obj but then i cant go further cause how do i add the next layer so i got stuck with arrays with one array in it the other posts i found about it i couldnt figure out maybe its just a real monday and i need to sleep over it. now i am at home and i can try post tomorrow code but its comlplettly useless cause the ideas where bad by me thats why i asked about an idea how to solve this in wich direction should i go how do i handle this the correct way – Limuz Aug 17 '20 at 17:52

1 Answers1

0

i'm not that familiar with php but here is a way i would solve the problem with JS


function doOrderSort(a, b) {
  return parseInt(a.displayOrder) - parseInt(b.displayOrder);
}

function findAndAppendSubs(item, originItems) {
  var allItems = Object.assign([], originItems);
  var subItems = allItems.filter(inner => inner.top_id === item.id);


  if (subItems !== undefined && subItems.length > 0) {
    for (var i = 0; i < subItems.length; i++) {
      subItems[i] = findAndAppendSubs(subItems[i], allItems)
    }
    item.subItems = subItems.sort(doOrderSort);
  }
  return item;

}


function sortAndAppendAllSubs(itemList) {

  var allItems = Object.assign([], itemList);
  var topItems = allItems.filter(inner => parseInt(inner.top_id) === 0);
  var resultList = [];
  if (topItems !== undefined && topItems.length > 0) {
    for (var it = 0; it < topItems.length; it++) {
      resultList.push(findAndAppendSubs(topItems[it], allItems));
    }
  }
  return resultList.sort(doOrderSort);
}

https://jsfiddle.net/3b4s79L5/ (open console to see the result:)

EDIT: added sort funktion and ignore if string or number as top_id

Kendoros
  • 16
  • 1
  • 1
  • Welcome to Stack Overflow. In future, if a question is tagged in one language, please don't provide an answer in another language. In this case it seems to have helped the OP but that that is rare case - in the majority of cases it is unhelpful, and you could get downvotes for not answering the actual question that was asked. Please note that both questions and answers on this site are meant to be helpful to future users also, so it helps to bear that in mind when giving an answer :) – FluffyKitten Aug 18 '20 at 17:34