您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP实现数组递归排序

2025/5/11 12:37:21发布13次查看
这次给大家带来php实现数组递归排序,php实现数组递归排序的注意事项有哪些,下面就是实战案例,一起来看一下。
本文实例讲述了php数组递归排序实现方法。分享给大家供大家参考,具体如下:
/**  * 递归根据特定key对数组排序  * @param $data  * @param string $orderkey  * @param string $sonkey  * @param int $orderby  * @return mixed  */ function recursion_orderby($data, $orderkey = 'order', $sonkey = 'children', $orderby = sort_asc) {   $func = function ($value) use ($sonkey, $orderkey, $orderby) {     if (isset($value[$sonkey]) && is_array($value[$sonkey])) {       $value[$sonkey] = recursion_orderby($value[$sonkey], $orderkey, $sonkey, $orderby);     }     return $value;   };   return array_orderby(array_map($func, $data), $orderkey, $orderby); } $a = [   [     'order' => 0,   ],   [     'order' => -1,     'children' => [       [         'order' => 0,       ],       [         'order' => -2,         'children' => [           ['order' => 0],           ['order' => -1],           ['order' => 1],         ],       ],     ],   ],   [     'order' => 2,   ], ]; var_dump(recursion_orderby($a)); /**  * 输出: array(3) {  [0] =>  array(2) {   'order' =>   int(-1)   'children' =>   array(2) {    [0] =>    array(2) {     'order' =>     int(-2)     'children' =>     array(3) {      [0] =>      array(1) {       'order' =>       int(-1)      }      [1] =>      array(1) {       'order' =>       int(0)      }      [2] =>      array(1) {       'order' =>       int(1)      }     }    }    [1] =>    array(1) {     'order' =>     int(0)    }   }  }  [1] =>  array(1) {   'order' =>   int(0)  }  [2] =>  array(1) {   'order' =>   int(2)  } } */
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
php回调函数及匿名函数使用详解
thinkphp实现微信支付(jsapi支付)流程教程详解_php实例
php实现微信支付和退款
以上就是php实现数组递归排序的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product