RecursiveIteratorIterator 最后一个孩子
我使用 RecursiveIteratorIterator 迭代多维数组,并希望能够知道当前元素是否是其深度的最后一个子元素。我想到了这一点:
$iterator = new RecursiveIteratorIterator($array,
RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $val) {
$next = clone $iterator;
$next->next();
$lastChild = ($next->getDepth() < $iterator->getDepth());
}
但是 RecursiveIteratorIterator 说它不可克隆。
I iterate through a multidimensional array with RecursiveIteratorIterator and would like to be able to know if the current element is the last child of it's depth. I thought about this:
$iterator = new RecursiveIteratorIterator($array,
RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $val) {
$next = clone $iterator;
$next->next();
$lastChild = ($next->getDepth() < $iterator->getDepth());
}
But the RecursiveIteratorIterator says it's not cloneable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“其深度的最后一个孩子”这句话并不是特别清楚。您能详细说明一下您的意思吗?其预期用途?
如果您的意思很简单,深度将在当前元素之后发生变化,那么(Recursive)CachingIterator有一个方便的hasNext方法来确定迭代器是否有任何进一步的项目。给出一个与问题中类似的淡化示例:
输出:
The phrase "last child of its depth" is not particularly clear. Could you elaborate on precisely what you mean; the intended use for this?
If you mean, quite simply, that the depth will change after the current element, then the
(Recursive)CachingIterator
has a handyhasNext
method to determine whether the iterator has any further items. To give a watered-down example similar to that in the question:Outputs: