关于xuzuning版主发的非递归树形数组构造函数有关问题
关于xuzuning版主发的非递归树形数组构造函数问题
之前自己使用过循环或者递归构建树型数组。今天搜索的时候看到之前xuzuning版主发的非递归树型数组构建函数。
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> /** * 创建子节点树形数组 * 参数 * $ar 数组,邻接列表方式组织的数据 * $id 数组中作为主键的下标或关联键名 * $pid 数组中作为父键的下标或关联键名 * 返回 多维数组 **/ function find_child($ar, $id='id', $pid='pid') { foreach($ar as $v) $t[$v[$id]] = $v; foreach ($t as $k => $item){ if( $item[$pid] ) { $t[$item[$pid]]['child'][$item[$id]] =& $t[$k]; } } return $t; }
觉得写的非常巧妙,效率很高,也非常安全,受益匪浅。但是,在实际使用中,碰见了一点小问题。
因为本函数的中心思想是通过$tree[$pid]这样的结构来存放,也就是说,每条记录的key为其$pid。
这样就出现了一个问题,我在使用json_encode()的时候,会变成
{"pid1":{"id":"0001","pid":"0000","children":[{"pid2":{"id":"0002","pid":"0001"}},{"pid3":{"id":"0003","pid":"0001"}}]}}这样
可是我需要的结构是
{"id":"0001","pid":"0000","children":[{"id":"0002","pid":"0001"},{"id":"0002","pid":"0001"}]}
也就是说,存放记录的key为array默认的。
请问这个函数应该怎么修改呢?
------解决方案--------------------
不大可能,除非你寻找出新的算法
这个算法就是利用了 pid 和 id 的关系,通过构造关联数组才得以实现的
如果要使用这个函数,就要接受这个结果。除去 child 键倒是可以的
如果你遍历结果,将所有 child 下的关联数组用 array_values 化为下标数组,亦是一种方法

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

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











If you find event ID 55, 50, 140 or 98 in the Event Viewer of Windows 11/10, or encounter an error that the disk file system structure is damaged and cannot be used, please follow the guide below to resolve the issue. What does Event 55, File system structure on disk corrupted and unusable mean? At session 55, the file system structure on the Ntfs disk is corrupted and unusable. Please run the chkMSK utility on the volume. When NTFS is unable to write data to the transaction log, an error with event ID 55 is triggered, which will cause NTFS to fail to complete the operation unable to write the transaction data. This error usually occurs when the file system is corrupted, possibly due to the presence of bad sectors on the disk or the file system's inadequacy of the disk subsystem.

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

In Alibaba software, once you successfully register an account, the system will assign you a unique ID, which will serve as your identity on the platform. But for many users, they want to query their ID, but don't know how to do it. Then the editor of this website will bring you detailed introduction to the strategy steps below. I hope it can help you! Where can I find the answer to Alibaba ID: [Alibaba]-[My]. 1. First open the Alibaba software. After entering the homepage, we need to click [My] in the lower right corner; 2. Then after coming to the My page, we can see [id] at the top of the page; Alibaba Is the ID the same as Taobao? Alibaba ID and Taobao ID are different, but the two
![Event ID 4660: Object deleted [Fix]](https://img.php.cn/upload/article/000/887/227/168834320512143.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Some of our readers encountered event ID4660. They're often not sure what to do, so we explain it in this guide. Event ID 4660 is usually logged when an object is deleted, so we will also explore some practical ways to fix it on your computer. What is event ID4660? Event ID 4660 is related to objects in Active Directory and will be triggered by any of the following factors: Object Deletion – A security event with Event ID 4660 is logged whenever an object is deleted from Active Directory. Manual changes – Event ID 4660 may be generated when a user or administrator manually changes the permissions of an object. This can happen when changing permission settings, modifying access levels, or adding or removing people or groups

Where can I check the Tencent Video ID? There is an exclusive ID in the Tencent Video APP, but most users do not know how to check the Tencent Video ID. Next is the graphic tutorial on how to check the Tencent Video ID brought by the editor for users who are interested. Users come and take a look! Tencent Video Usage Tutorial Where to check Tencent Video ID 1. First open the Tencent Video APP and enter the special area through [Personal Center] in the lower right corner of the main page; 2. Then enter the Personal Center page and select the [Settings] function; 3. Then go to Settings page, click [Exit Account] at the bottom; 4. Finally, you can view the exclusive ID number on the page shown below.

Vue cannot obtain the id attribute because getElementById is used in the "created()" hook function, and Vue has not completed mounting; the solution is to "created() {let serachBox = document.getElementById('searchBox') ;...}" code can be migrated to the "mounted()" hook function.

Many friends may be unfamiliar with the pid identifier, and you can check it in the task manager. However, some users cannot find the PID identifier when they open the Task Manager. In fact, if the user wants to view the process PID identifier, he or she needs to set the relevant settings of the "Task Manager" to see it. The following editor will take the win7 system as an example. How to view the process PID identifier. The PID identifier is a unique sequential number automatically assigned by the Windows operating system to running programs. After the process is terminated, the PID is recycled by the system and may continue to be assigned to newly running programs. When users need to view the process, they will use the task Manager to check, so how to check the process PID identifier? Let me share it with you below

How to cancel the Apple ID that is not yours. To cancel the Apple ID that is not yours, you can follow the steps below: First, click "Settings" on the mobile phone interface, then find the "iTunesStore and AppStore" option, and click to enter. Next, select the Apple ID previously set on your phone in the pop-up window and select the "Log Out" option. In this way, you can cancel the Apple ID that is not yours. 2. If you still cannot log out or log out, you can try to contact after-sales customer service. The method to unlock iPad operation is as follows: Make sure that your own Apple ID is displayed in the settings. If it is someone else's ID, you can log out directly. 4. First find it on our mobile desktop
