构建递归删除函数(在 php 中)
事情是这样的。我有一个“树”或“子树”,我想导航并删除其中的每个元素。每个“节点”可能包含指向其下方其他节点的链接(没问题),或者可能包含该特定“树”之外的链接/“子树”。如何构建一个仅删除指定树“内部”的函数?
Here's the deal. I've got a "tree" or a "subtree" that I want to navigate and delete every element in. Each "node" may contain links to other nodes below it (no problem) OR may contain links OUTSIDE that particular "tree"/"subtree". How can I build a function that only deletes "within" the specified tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这与您习惯的递归删除相同。您只需将链接分开 - 一个列表用于树内链接,一个列表用于树外链接。或者,您可以使用一个标志来跟踪每个链接的树内/树外状态 - 但您必须在创建链接时进行区分。
This is the same recursive delete that you're used to. You just have to keep your links separated - one list for in-tree links, one for out-of-tree links. Alternately, you can have a flag that keeps track of the in-tree/out-of-tree state for each link - but you're going to have to distinguish when you make the link.
你需要 RecursiveIterator
you need RecursiveIterator
您需要使用
realpath()
:上面的内容应该可以满足您的要求。
哦...我刚刚意识到这可能与文件系统无关...错误都是你的! :P
You need to use
realpath()
:The above should do what you're looking for.
Oh... I just realized this may not be related to the filesystem... The fault is all yours! :P