首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RecursiveIterator不返回任何对象

RecursiveIterator不返回任何对象
EN

Stack Overflow用户
提问于 2016-04-01 18:13:04
回答 1查看 45关注 0票数 1

我有一个不返回特定数组结构中的对象/元素的递归函数:

尝试使用递归的数组im如下:

代码语言:javascript
复制
$feedStruct = [database] => Array
    (
        [property] => Array
            (
                [0] => Array
                    (
                        [title] => Array
                            (
                            )

                        [tipology] => Array
                            (
                            )

                        [description] => Array
                            (
                            )

                        [location] => Array
                            (
                            )

                        [year] => Array
                            (
                            )

                        [price] => Array
                            (
                            )

                        [area_plot] => Array
                            (
                            )

                        [habitable_area] => Array
                            (
                            )

                        [ref] => Array
                            (
                            )

                        [video] => Array
                            (
                            )

                        [default_photo] => Array
                            (
                            )

                        [photo1] => Array
                            (
                            )

                    )

                [1] => Array
                    (
                        [title] => Array
                            (
                            )

                        [tipology] => Array
                            (
                            )

                        [description] => Array
                            (
                            )

                        [location] => Array
                            (
                            )

                        [year] => Array
                            (
                            )

                        [price] => Array
                            (
                            )

                        [area_plot] => Array
                            (
                            )

                        [habitable_area] => Array
                            (
                            )

                        [ref] => Array
                            (
                            )

                        [video] => Array
                            (
                            )

                        [default_photo] => Array
                            (
                            )

                        [photo1] => Array
                            (
                            )

                    )

            )

    )

代码如下:

代码语言:javascript
复制
     $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($feedStruct));
     $keys = array();
     foreach ($iterator as $key => $value) {
         // Build long key name based on parent keys
         for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {

          $val = $iterator->getSubIterator($i)->key();

          }
     }

当运行上面的数组时,它不会迭代,对于其他数组,我没有问题,你知道为什么吗?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-04-01 23:58:40

您的$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($feedStruct));将被解释为$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($feedStruct), RecursiveIteratorIterator::SELF_FIRST);,因为RecursiveIteratorIterator::LEAVES_ONLY是三个参数中的默认参数(即RecursiveIteratorIterator::SELF_FIRSTRecursiveIteratorIterator::CHILD_FIRST)。

我将尝试解释参数。假设您有下面的数组

代码语言:javascript
复制
$array = array(
    0 => array(
        'city' => 'Jos',
        'places_to_visit' => array(
            'Lagos',
            'Capetown'
        )
    ),
    1 => "sd"
);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($feedStruct), $mode);
$keys = array();
foreach ($iterator as $key => $value) {
// Build long key name based on parent keys
    for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
        $val[] = $iterator->getSubIterator($i)->key();
    }
}

RecursiveIteratorIterator::LEAVES_ONLY表示只迭代每个结构中的最后一个值。在上述数组$array的情况下,如果为$mode = RecursiveIteratorIterator::LEAVES_ONLY

代码语言:javascript
复制
print_r($val); 
/* Outputs
city => Jos
0 => Lagos
1 => Capetown
1 => sd */

RecursiveIteratorIterator::CHILD_FIRST表示先迭代子值,然后再迭代父值。在上述数组$array的情况下,如果为$mode = RecursiveIteratorIterator::CHILD_FIRST

代码语言:javascript
复制
print_r($val); 
/* Outputs
city => Jos
0 => Lagos
1 => Capetown
places_to_visit => Array()
0 => Array()
1 => sd */

RecursiveIteratorIterator::SELF_FIRST意味着只迭代每个结构中的父值。在上述数组$array的情况下,如果为$mode = RecursiveIteratorIterator::SELF_FIRST

代码语言:javascript
复制
print_r($val); 
/*Outputs 
0 => Array()
city => Jos
places_to_visit => Array()
0 => Lagos
1 => Capetown
1 => sd */

在您的示例中,$iterator将返回,因为在您的结构$feedStruct中没有叶子(最终值)。试试RecursiveIteratorIterator::SELF_FIRST,效果很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36353650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档