如何使用键移动多个PHP数组元素?

时间:2012-12-28 14:58:59

标签: php arrays sorting

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

数组:

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

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

A
  A.A
   A.A.A
B
C
D

结果:

Array
   (
       [0] => Array
           (
               [cms_parent_node] => 0
               [cms_before] =>
               [cms_after] =>
               [cms_guid] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
               [cms_title] => Dossier A
               [level] => Array
                   (
                       [0] => Array
                           (
                               [cms_parent_node] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
                               [cms_before] =>
                               [cms_after] =>
                               [cms_guid] => D94840DC-7CD8-E7AA-7C69-A237306D24E6
                               [cms_title] => Dossier A.A
                               [level] => Array
                                   (
                                       [0] => Array
                                           (
                                               [cms_parent_node] => D94840DC-7CD8-E7AA-7C69-A237306D24E6
                                               [cms_before] =>
                                               [cms_after] =>
                                               [cms_guid] => 1B20700B-70F0-FFE5-47AC-3DA35460C702
                                               [cms_title] => Dossier A.A.A
                                               [level] => Array
                                                   (
                                                   )

                                           )

                                   )

                           )

                   )

           )


    [1] => Array
        (
            [cms_parent_node] => 0
            [cms_before] => 
            [cms_after] => 0E3B180C-F4AA-484F-A19D-318E6D4D2A6C
            [cms_guid] => 1CBF252E-0C28-36E9-D5DF-66F361FB00E9
            [cms_title] => Dossier B
            [level] => Array
                (
                )

        )

       [2] => Array
           (
               [cms_parent_node] => 0
               [cms_before] =>
               [cms_after] => 1CBF252E-0C28-36E9-D5DF-66F361FB00E9
               [cms_guid] => 57F64452-B6BE-3039-F30A-1BE413936A71
               [cms_title] => Dossier C
               [level] => Array
                   (
                   )

           )

       [3] => Array
           (
               [cms_parent_node] => 0
               [cms_before] =>
               [cms_after] => 57F64452-B6BE-3039-F30A-1BE413936A71
               [cms_guid] => 44269A70-FF01-EB17-7437-8B1EFB746E0D
               [cms_title] => Dossier D
               [level] => Array
                   (
                   )

           )


   )

1 个答案:

答案 0 :(得分: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;
}

处理你提问技巧的问题......