需求是这样的,原数组如下,通过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。

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

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