如何更改菜单的数组?

时间:2018-10-26 16:24:42

标签: php arrays arraylist

Rest API获得了一系列数据。我正在尝试从这些数据制作菜单,但是为此,我需要就嵌套进行转换(我认为这是循环完成的)

我在互联网上翻遍,到处都有通常的数字id递归,而我的id是一个字符串,结果我尝试通过foreach自己完成,

 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);

我写的循环效果不佳,它只显示了一个直到2级的分支

$ elements数组在下面的print_r和var_export中传播

[objectId]是商品的ID

[parent]是元素的父级,它引用父级的objectId,如果为空或等于0,则没有父级

提出提供无限嵌套解决方案的强烈要求。

我希望你能帮助我。

通过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
                         )

                 )

         )

   )

通过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',
       ),
     ),
   ),
 )

我需要格式数组

格式示例

 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 个答案:

答案 0 :(得分:0)

我相信您想遍历所有项目,当前项目为$current。如果它具有父对象,请找到父对象$parent,然后将该项目添加到父对象的子数组($parent.children[] = $current)。

// Loop through all
foreach ($elements as $current) {
  if (!empty($current['parent'])) {
    // Loop through all if they have a parent
    foreach ($elements as $parent) {
      // Add $current as a child if this is the parent
      if ($current['parent'] === $parent['objectId']) {
        $parent['children'][] = $current;
      }
    }
  }
}

如果只希望在数组中有父项,则可以通过检查元素是否没有父项来unset()或仅创建一个新数组。

答案 1 :(得分:0)

尝试一下:

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();
}

您可以这样使用它:

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

$result将充满objectId键控元素层次结构列表,其中每个元素在children键下具有其子元素。

此功能应可用于无限量的嵌套。