doumiang0597 2018-10-26 16:24
浏览 71
已采纳

如何更改菜单的数组?

There is an array of data obtained by Rest API. I am trying to make a menu from this data, but for this I need to convert it with regard to nesting (I think this is done in a loop)

I rummaged through the Internet, everywhere recursion with usual numeric id, and my id is a string, I tried to do it myself through foreach, it turned out

 foreach ($elements as $item){

     $thisRef = &$ref[$item['objectId']];

     $thisRef['parent'] = &$item['parent'];
     $thisRef['name'] = $item['property']['ru']['value'];
     $thisRef['objectId'] = $item['objectId'];
     $thisRef['number'] = $item['number'];
   $thisRef['duration'] = $item['duration'];
     $thisRef['object'] = $item['object'];
     $thisRef['listorder'] = $item['listorder'];

    if($item['parent'] == 0) {
         $items[$item['objectId']] = &$thisRef;
    } else {

    $ref[$item['parent']]['child'][$item['objectId']] = &$thisRef;       



    }


  }



 print_r($items);

The cycle written by me works poorly, it displays a branch only up to level 2

array of $elements spread below in print_r and var_export

[objectId] is the id of the item

[parent] is the parent of the element, it refers to the parent's objectId, if it is empty or equal to 0, then it has no parent

A big request to offer a solution with unlimited nesting.

I hope you are will help me.

Array through print_r

   Array
    (
     [0] => Array
         (
             [parent] => D0384DBB-F67C-FE92-FF46-F192B3F83B00
             [created] => 1540568545112
             [element_id] => 
             [ownerId] => 
             [type] => 
             [duration] => 
             [number] => 1
             [start_time] => 
             [enable] => 
             [tiam] => 
             [listorder] => 1
             [quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
             [updated] => 1540569546246
             [value] => 
             [objectId] => 0DE56A96-9FA9-64E0-FFAB-97A246517200
             [object] => round
             [___class] => Elements
             [property] => Array
                 (
                     [ru] => Array
                         (
                             [language_code] => ru
                             [created] => 1540568545865
                             [lang_id] => 0E5BD427-7883-903C-FFDD-2309E2D79800
                             [element_id] => 0DE56A96-9FA9-64E0-FFAB-97A246517200
                             [quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
                             [type] => 
                             [ownerId] => 
                             [value] => Раунд
                             [updated] => 
                             [objectId] => C28EE86F-71BC-A6D3-FFF6-707638303500
                             [object] => round
                             [___class] => Elements_values
                         )

                 )

         )

     [1] => Array
         (
             [parent] => 
             [created] => 1540540322673
             [element_id] => 
             [ownerId] => 
             [type] => 
             [duration] => 
             [number] => 1
             [start_time] => 
             [enable] => 
             [tiam] => 
             [listorder] => 1
             [quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
             [updated] => 1540564573693
             [value] => 
             [objectId] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
             [object] => team
             [___class] => Elements
             [property] => Array
                 (
                     [ru] => Array
                         (
                             [language_code] => ru
                             [created] => 1540540323417
                             [lang_id] => 0E5BD427-7883-903C-FFDD-2309E2D79800
                             [element_id] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
                             [quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
                             [type] => 
                             [ownerId] => 
                             [value] => Команда
                             [updated] => 
                             [objectId] => 2B2C86CF-E1F8-5D2C-FF0E-1DB592CC7500
                             [object] => team
                             [___class] => Elements_values
                         )

                 )

         )

     [2] => Array
         (
             [parent] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
             [created] => 1540567572958
             [element_id] => 
             [ownerId] => 
             [type] => 
             [duration] => 
             [number] => 1
             [start_time] => 
             [enable] => 
             [tiam] => 
             [listorder] => 1
             [quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
             [updated] => 1540567674259
             [value] => 
             [objectId] => D0384DBB-F67C-FE92-FF46-F192B3F83B00
             [object] => stage
             [___class] => Elements
             [property] => Array
                 (
                     [ru] => Array
                         (
                             [language_code] => ru
                             [created] => 1540567573722
                             [lang_id] => 0E5BD427-7883-903C-FFDD-2309E2D79800
                             [element_id] => D0384DBB-F67C-FE92-FF46-F192B3F83B00
                             [quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
                             [type] => 
                             [ownerId] => 
                             [value] => Этап
                             [updated] => 
                             [objectId] => 4E694471-70B3-C12B-FF79-97895532DD00
                             [object] => stage
                             [___class] => Elements_values
                         )

                 )

         )

     [3] => Array
         (
             [parent] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
             [created] => 1540565297592
             [element_id] => 
             [ownerId] => 
             [type] => 
             [duration] => 
             [number] => 1
             [start_time] => 
             [enable] => 
             [tiam] => 
             [listorder] => 1
             [quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
             [updated] => 1540565310503
             [value] => 
             [objectId] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
             [object] => route
             [___class] => Elements
             [property] => Array
                 (
                     [ru] => Array
                         (
                             [language_code] => ru
                             [created] => 1540565298342
                             [lang_id] => 0E5BD427-7883-903C-FFDD-2309E2D79800
                             [element_id] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
                             [quest_id] => E18A9589-6AB2-8304-FFFC-98C11314CD00
                             [type] => 
                             [ownerId] => 
                             [value] => Маршрут
                             [updated] => 
                             [objectId] => 9CD62B6E-EACF-2911-FF57-3FF369F2BE00
                             [object] => route
                             [___class] => Elements_values
                         )

                 )

         )

   )

Array through var_export

array (
    0 => 
   array (
     'parent' => 'D0384DBB-F67C-FE92-FF46-F192B3F83B00',
     'created' => 1540568545112,
     'element_id' => NULL,
     'ownerId' => NULL,
     'type' => NULL,
     'duration' => NULL,
     'number' => '1',
     'start_time' => NULL,
     'enable' => NULL,
     'tiam' => NULL,
     'listorder' => '1',
     'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
     'updated' => 1540569546246,
     'value' => NULL,
     'objectId' => '0DE56A96-9FA9-64E0-FFAB-97A246517200',
     'object' => 'round',
     '___class' => 'Elements',
     'property' => 
     array (
       'ru' => 
       array (
         'language_code' => 'ru',
         'created' => 1540568545865,
         'lang_id' => '0E5BD427-7883-903C-FFDD-2309E2D79800',
         'element_id' => '0DE56A96-9FA9-64E0-FFAB-97A246517200',
         'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
         'type' => NULL,
         'ownerId' => NULL,
         'value' => 'Раунд',
         'updated' => NULL,
         'objectId' => 'C28EE86F-71BC-A6D3-FFF6-707638303500',
         'object' => 'round',
         '___class' => 'Elements_values',
       ),
     ),
   ),
   1 => 
   array (
     'parent' => NULL,
     'created' => 1540540322673,
     'element_id' => NULL,
     'ownerId' => NULL,
     'type' => NULL,
     'duration' => NULL,
     'number' => '1',
     'start_time' => NULL,
     'enable' => NULL,
     'tiam' => NULL,
     'listorder' => '1',
     'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
     'updated' => 1540564573693,
     'value' => NULL,
     'objectId' => '4449513D-1CDD-00EA-FFDF-1947E4FDEA00',
     'object' => 'team',
     '___class' => 'Elements',
     'property' => 
     array (
       'ru' => 
       array (
         'language_code' => 'ru',
         'created' => 1540540323417,
         'lang_id' => '0E5BD427-7883-903C-FFDD-2309E2D79800',
         'element_id' => '4449513D-1CDD-00EA-FFDF-1947E4FDEA00',
         'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
         'type' => NULL,
         'ownerId' => NULL,
         'value' => 'Команда',
         'updated' => NULL,
         'objectId' => '2B2C86CF-E1F8-5D2C-FF0E-1DB592CC7500',
         'object' => 'team',
         '___class' => 'Elements_values',
       ),
     ),
   ),
   2 => 
   array (
     'parent' => 'F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00',
     'created' => 1540567572958,
     'element_id' => NULL,
     'ownerId' => NULL,
     'type' => NULL,
     'duration' => NULL,
     'number' => '1',
     'start_time' => NULL,
     'enable' => NULL,
     'tiam' => NULL,
     'listorder' => '1',
     'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
     'updated' => 1540567674259,
     'value' => NULL,
     'objectId' => 'D0384DBB-F67C-FE92-FF46-F192B3F83B00',
     'object' => 'stage',
     '___class' => 'Elements',
     'property' => 
     array (
       'ru' => 
       array (
         'language_code' => 'ru',
         'created' => 1540567573722,
         'lang_id' => '0E5BD427-7883-903C-FFDD-2309E2D79800',
         'element_id' => 'D0384DBB-F67C-FE92-FF46-F192B3F83B00',
         'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
         'type' => NULL,
         'ownerId' => NULL,
         'value' => 'Этап',
         'updated' => NULL,
         'objectId' => '4E694471-70B3-C12B-FF79-97895532DD00',
         'object' => 'stage',
         '___class' => 'Elements_values',
       ),
     ),
   ),
   3 => 
   array (
     'parent' => '4449513D-1CDD-00EA-FFDF-1947E4FDEA00',
     'created' => 1540565297592,
     'element_id' => NULL,
     'ownerId' => NULL,
     'type' => NULL,
     'duration' => NULL,
     'number' => '1',
     'start_time' => NULL,
     'enable' => NULL,
     'tiam' => NULL,
     'listorder' => '1',
     'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
     'updated' => 1540565310503,
     'value' => NULL,
     'objectId' => 'F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00',
     'object' => 'route',
     '___class' => 'Elements',
     'property' => 
     array (
       'ru' => 
       array (
         'language_code' => 'ru',
         'created' => 1540565298342,
         'lang_id' => '0E5BD427-7883-903C-FFDD-2309E2D79800',
         'element_id' => 'F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00',
         'quest_id' => 'E18A9589-6AB2-8304-FFFC-98C11314CD00',
         'type' => NULL,
         'ownerId' => NULL,
         'value' => 'Маршрут',
         'updated' => NULL,
         'objectId' => '9CD62B6E-EACF-2911-FF57-3FF369F2BE00',
         'object' => 'route',
         '___class' => 'Elements_values',
       ),
     ),
   ),
 )

I need format array

Format example

 Array
   (

 ......

     [4449513D-1CDD-00EA-FFDF-1947E4FDEA00] => Array
         (
             [parent] => 
             [name] => Команда
             [objectId] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
             [number] => 1
             [duration] => 
             [object] => team
             [listorder] => 1
             [child] => Array
                 (
                     [F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00] => Array
                         (
                             [parent] => 4449513D-1CDD-00EA-FFDF-1947E4FDEA00
                             [name] => Маршрут
                             [objectId] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
                             [number] => 1
                             [duration] => 
                             [object] => route
                             [listorder] => 1
                             [child]=> Array

                                            (
                                                [D0384DBB-F67C-FE92-FF46-F192B3F83B00] => Array
                                                    (
                                                        [parent] => F0BFCFFA-D8F7-B5BE-FF37-D92AE35BBD00
                                                        [name] => Этап
                                                        [objectId] => D0384DBB-F67C-FE92-FF46-F192B3F83B00
                                                        [number] => 1
                                                        [duration] => 
                                                        [object] => stage
                                                        [listorder] => 1
                                                    )



                             )


                         )

                 )

 ....

         )



 )
  • 写回答

2条回答 默认 最新

  • dounie0889 2018-10-26 20:59
    关注

    Try this:

    function cascadeElements($elements, &$result, $index = 0, $parent = null) {       
        $next = function () use ($elements, &$result, $index, $parent) {
            cascadeElements($elements, $result, ++$index, $parent);
        };
    
        if ($index >= count($elements)) {
            return;
        }
    
        $element = $elements[$index];
    
        if (!$parent && $element['parent'] !== null) {           
            return $next();
        }
    
        if ($parent && $element['parent'] !== $parent['objectId']) {
            return $next();
        }
    
        $element['children'] = [];
    
        cascadeElements($elements, $element['children'], 0, $element);
    
        $result[$element['objectId']] = $element;
    
        return $next();
    }
    

    You use it like this:

    $result = [];
    cascadeElements($data, $result);
    

    $result will get filled with objectId keyed hierarchical list of elements where each element has it's children under children key.

    This function should work for infinite amount of nesting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题