为了账号安全,请及时绑定邮箱和手机立即绑定

如何递归排序关联数组

如何递归排序关联数组

PHP
犯罪嫌疑人X 2021-06-29 13:54:48
我想对以下关联数组进行排序:$tree = [  "id" => 245974,  "children" => [    [      "id" => 111    ],    [      "id" => 245982,      "children" => [        [          "id" => 246093,          "children" => [            [              "id" => 225892            ],            [              "id" => 225893            ],            [              "id" => 225902            ]          ]        ]      ]    ]  ]];在id=>的“搜索值”之后所需的排序顺序225902:[  "id" => 245974,  "children" => [    [      "id" => 245982, // <-- this is moved up      "children" => [        [          "id" => 246093,          "children" => [            [              "id" => 225902 // <-- this is moved up            ],            [              "id" => 225892            ],            [              "id" => 225893            ]          ]        ]      ]    ],    [      "id" => 111    ]  ]];我试过的:<?php$category_id = 225902;function custom_sort(&$a, &$b) {  global $category_id;  if ($a['id'] === $category_id) {    return -1;  }  if ($b['id'] === $category_id) {    return 1;  }  if (array_key_exists('children', $a)) {    if (usort($a['children'], "custom_sort")) {      return -1;    }  }  if (array_key_exists('children', $b)) {    if (usort($b['children'], "custom_sort")) {      return 1;    }  }  return 0;}function reorder_tree($tree) {  usort($tree['children'], "custom_sort");  return $tree;}echo "<pre>";var_dump(reorder_tree($tree));echo "</pre>";
查看完整描述

1 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

伟大的尝试,而且非常在正确的轨道上。比较器中递归的问题在于,usort当数组长度为 1 时不会调用比较器函数,因此是否探索整棵树是usort. 这将放弃id => 245982树的分支。


解决方案是避免usort直接在的比较器函数中递归。相反,使用usort根据需要调用的常规递归函数,即当前数组或子数组包含目标 id。我使用一个单独的数组来跟踪哪些元素应该向前移动,但是如果您愿意,您可以跳出循环并将单个元素拼接/取消移动到前面。


我们还可以为$category_id函数设置一个参数。


这是一种方法:


function reorder_tree_r(&$children, $target) {

    $order = [];

    $should_sort = false;


    foreach ($children as $i => &$child) {

        $order[$i] = false;


        if (array_key_exists("children", $child) &&

            reorder_tree_r($child["children"], $target) || 

            $child["id"] === $target) {

            $order[$i] = true;

            $should_sort = true;

        }

    }


    if ($should_sort) {

        $priority = [];

        $non_priority = [];


        for ($i = 0; $i < count($children); $i++) {

            if ($order[$i]) {

                $priority[]= $children[$i];

            }

            else {

                $non_priority[]= $children[$i];

            }

        }


        $children = array_merge($priority, $non_priority);

    }


    return $should_sort;

}


function reorder_tree($tree, $target) {

    if (!$tree || !array_key_exists("children", $tree)) {

        return $tree;

    }


    reorder_tree_r($tree["children"], $target);

    return $tree;

}



var_export(reorder_tree($tree, 225902));

输出:


array (

  'id' => 245974,

  'children' => 

  array (

    0 => 

    array (

      'id' => 245982,

      'children' => 

      array (

        0 => 

        array (

          'id' => 246093,

          'children' => 

          array (

            0 => 

            array (

              'id' => 225902,

            ),

            1 => 

            array (

              'id' => 225892,

            ),

            2 => 

            array (

              'id' => 225893,

            ),

          ),

        ),

      ),

    ),

    1 => 

    array (

      'id' => 111,

    ),

  ),


查看完整回答
反对 回复 2021-07-02
  • 1 回答
  • 0 关注
  • 126 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信