PHP反向递归
需求是这样的,原数组如下,通过chrome配合phpview的插件截图(二维数组)
经过“反向递归后”需要展现成如下树状结构。
其中parent_id是level为上一级的id(最顶级的level为0),比如parent_id=125的用户level为1,那么他的上一级的id是125,先谢过了
回复内容:
需求是这样的,原数组如下,通过chrome配合phpview的插件截图(二维数组)
经过“反向递归后”需要展现成如下树状结构。
其中parent_id是level为上一级的id(最顶级的level为0),比如parent_id=125的用户level为1,那么他的上一级的id是125,先谢过了
不好意思,回答晚了,做了个稍微复杂点的,用了一个内部排序:
<code>// $formated_arr 是你的输入对象 // 首先按照level做一次排序 usort($formated_arr, function($a, $b) { $al = intval($a['level']); $bl = intval($b['level']); return ($al > $bl) ? 1 : -1; }); // 因为上面做过排序了,所以这里虽然是递归,但对数组只遍历了一次 function reformat_tree(&$arrTmp, $parent_id=0) { $ret = null; foreach ($arrTmp as $k => $v) { if($v['parent_id'] == $parent_id) { $ret[$v['id']] = $v; unset($arrTmp[$k]); $child = reformat_tree($arrTmp, $v['id']); !is_null($child) ? $ret[$v['id']]['child'] = $child : 1; } } return $ret; } echo(json_encode(reformat_tree($formated_arr)));</code>
试试我这个:
<code>function createMenuTree($data = array(), $pid = 0){ if (empty($data)){ return array(); } static $level = 1; $returnArray = array(); foreach ($data as $node){ if ($node['parent_id'] == $pid){ $returnArray[] = array( 'cat_id' => $node['cat_id'], 'cat_name' => $node['cat_name'], 'level' => $level, 'parent_id' => $node['parent_id'], 'show_in_nav' => $node['show_in_nav'], 'is_show' => $node['is_show'], 'sort_order' => $node['sort_order'] ); if (hasChild($node['cat_id'], $data)){ $level++; $returnArray = array_merge($returnArray, createMenuTree($data, $node['cat_id'])); $level--; } } } return $returnArray; } function hasChild($cid, $data){ $hasChild = false; foreach ($data as $node){ if ($node['parent_id'] == $cid){ $hasChild = true; break; } } return $hasChild; } </code>
字段跟你的不是很一样,但是思路好像跟你想要的差不多,你可以自己拿去修改一下。
<code>function recursive_tree($arr,$level = 0,$parent_id = 0){ $tmp = array(); foreach ($arr as $key => $value) { if($value['level'] == $level && $value['parent_id'] == $parent_id){ $arr[$key]['child'] = recursive_tree($arr,$value['level'] + 1,$value['id']); if(empty($arr[$key]['child'])){ unset($arr[$key]['child']); } $tmp[] = $arr[$key]; } } return $tmp; } $arr = array( array( 'id' => '125', 'level' => '0', 'user_id' => '1021', 'parent_id' => '0', ), array( 'id' => '189', 'level' => '1', 'user_id' => '1022', 'parent_id' => '125', ), array( 'id' => '425', 'level' => '2', 'user_id' => '4119', 'parent_id' => '189', ), array( 'id' => '385', 'level' => '3', 'user_id' => '3170', 'parent_id' => '425', ), array( 'id' => '782', 'level' => '3', 'user_id' => '5698', 'parent_id' => '425', ), array( 'id' => '688', 'level' => '1', 'user_id' => '7045', 'parent_id' => '125', ) ); echo json_encode(recursive_tree($arr,0,0));die;</code>
http://segmentfault.com/q/1010000004052822/a-1020000004055246
参照我在这个问题下的回答的前半部分,详细解释了题主的问题所需要的那个并不复杂的算法,只是题目从OC变成了PHP。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
