Search code examples
phpstdclass

Array with Objects add to new Array as Child


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


Solution

  • 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