-4

我需要使用 cms_before 或 cms_after 键从数组中移动元素。例如,文件夹 B 在使用 GUID (0E3B180C-F4AA-484F-A19D-318E6D4D2A6C) 的文件夹 A 之后。

数组:

大批
(
    [0] => 数组
        (
            [cms_parent_node] => 0
            [cms_before] =>
            [cms_after] =>
            [cms_guid] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
            [cms_title] => 档案 A
            [级别] => 数组
                (
                    [0] => 数组
                        (
                            [cms_parent_node] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
                            [cms_before] =>
                            [cms_after] =>
                            [cms_guid] => D94840DC-7CD8-E7AA-7C69-A237306D24E6
                            [cms_title] => 档案 AA
                            [级别] => 数组
                                (
                                    [0] => 数组
                                        (
                                            [cms_parent_node] => D94840DC-7CD8-E7AA-7C69-A237306D24E6
                                            [cms_before] =>
                                            [cms_after] =>
                                            [cms_guid] => 1B20700B-70F0-FFE5-47AC-3DA35460C702
                                            [cms_title] => 档案 AAA
                                            [级别] => 数组
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => 数组
        (
            [cms_parent_node] => 0
            [cms_before] =>
            [cms_after] => 1CBF252E-0C28-36E9-D5DF-66F361FB00E9
            [cms_guid] => 57F64452-B6BE-3039-F30A-1BE413936A71
            [cms_title] => 档案 C
            [级别] => 数组
                (
                )
        )
    [2] => 数组
        (
            [cms_parent_node] => 0
            [cms_before] =>
            [cms_after] => 57F64452-B6BE-3039-F30A-1BE413936A71
            [cms_guid] => 44269A70-FF01-EB17-7437-8B1EFB746E0D
            [cms_title] => 档案 D
            [级别] => 数组
                (
                )
        )
    [3] => 数组
        (
            [cms_parent_node] => 0
            [cms_before] =>
            [cms_after] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
            [cms_guid] => 1CBF252E-0C28-36E9-D5DF-66F361FB00E9
            [cms_title] => 档案 B
            [级别] => 数组
                (
                )
        )
)

返回数组必须按以下顺序:

一个
  AA
   AAA
乙
C
D

结果:

大批
   (
       [0] => 数组
           (
               [cms_parent_node] => 0
               [cms_before] =>
               [cms_after] =>
               [cms_guid] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
               [cms_title] => 档案 A
               [级别] => 数组
                   (
                       [0] => 数组
                           (
                               [cms_parent_node] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
                               [cms_before] =>
                               [cms_after] =>
                               [cms_guid] => D94840DC-7CD8-E7AA-7C69-A237306D24E6
                               [cms_title] => 档案 AA
                               [级别] => 数组
                                   (
                                       [0] => 数组
                                           (
                                               [cms_parent_node] => D94840DC-7CD8-E7AA-7C69-A237306D24E6
                                               [cms_before] =>
                                               [cms_after] =>
                                               [cms_guid] => 1B20700B-70F0-FFE5-47AC-3DA35460C702
                                               [cms_title] => 档案 AAA
                                               [级别] => 数组
                                                   (
                                                   )

                                           )

                                   )

                           )

                   )

           )


    [1] => 数组
        (
            [cms_parent_node] => 0
            [cms_before] =>
            [cms_after] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
            [cms_guid] => 1CBF252E-0C28-36E9-D5DF-66F361FB00E9
            [cms_title] => 档案 B
            [级别] => 数组
                (
                )

        )

       [2] => 数组
           (
               [cms_parent_node] => 0
               [cms_before] =>
               [cms_after] => 1CBF252E-0C28-36E9-D5DF-66F361FB00E9
               [cms_guid] => 57F64452-B6BE-3039-F30A-1BE413936A71
               [cms_title] => 档案 C
               [级别] => 数组
                   (
                   )

           )

       [3] => 数组
           (
               [cms_parent_node] => 0
               [cms_before] =>
               [cms_after] => 57F64452-B6BE-3039-F30A-1BE413936A71
               [cms_guid] => 44269A70-FF01-EB17-7437-8B1EFB746E0D
               [cms_title] => 档案 D
               [级别] => 数组
                   (
                   )

           )


   )
4

1 回答 1

0

像这样的东西。

function reorder($folders) {
  $current = null;
  $after = array();

  foreach ($folders as &$folder) {
    if ($folder['cms_after']) $after[$folder['cms_after']] =& $folder;
      else $current =& $folder;
  }

  $list = array();
  while ($current) {
    $list[] = $current;
    $current = isset($after[$current['cms_guid']]) ? $after[$current['cms_guid']] : null;
  }

  return $list;
}

锻炼你的提问技巧......

于 2012-12-28T15:13:47.057 回答