PHP以递归方式获取父键

时间:2018-01-05 23:36:09

标签: php

对于以下对象数组中的所有非数字键,我试图获取直接父键。如果直接父键是第一级索引,那么我只是得到null。这将在大批量数据上运行,因此性能有点重要。 $ values可以包含许多对象,而不仅仅是这个示例中的一个。

在这里编辑了一些json,略有不同的数据/结构但适用相同的规则https://pastebin.com/raw/1ZyGZPCr

$values = Array
(
    [0] => stdClass Object
        (
            [id] => 7320340
            [name] => 1373377
            [images] => Array
                (
                    [0] => file1.jpg
                    [1] => file2.jpg
                )
            [attributes] => stdClass Object
                (
                    [height] => 1
                    [width] => 3
                )
            [info] => stdClass Object
                (
                    [value] => 123
                    [location] => stdClass Object
                    (
                        [postal] => stdClass Object
                        (
                            [country] => stdClass Object
                            (
                                [name] => Australia
                                [code] => AUS
                            )
                        )
                    )
                )                    

        )

)

所需的输出将是

id:null
name:null
images:null
attributes:null
height:attributes
width:attributes
info:null
value:info
location:info
postal:location
country:postal
name:country
code:country

请注意,对于带有数字键的对象(例如图片),我不需要父键。我的代码到目前为止

$array_iterator = new RecursiveArrayIterator( $values );
$iterator_iterator = new RecursiveIteratorIterator( $array_iterator, RecursiveIteratorIterator::LEAVES_ONLY );
foreach ( $iterator_iterator as $key => $value ) {
    for ( $i = 0; $i < $iterator_iterator->getDepth()+1; $i++ ) {    
        $k = $iterator_iterator->getSubIterator($i)->key();
        if(!is_int($k)) {
            // if current element is array, then get key of object one level up ... ?
        }
    }
}

1 个答案:

答案 0 :(得分:1)

也许只是一个标准的递归函数就足够了?我要发布你发布的json字符串:

function recParent($arr,&$new,$parent=false)
{
    foreach($arr as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key))
                $new[]  =   (!is_numeric($parent) && !empty($parent))? $key.':'.$parent : $key.':null';
            recParent($value,$new,$key);
        }
        else {
            if(!is_numeric($key))
                $new[]  =   (!is_numeric($parent) && !empty($parent))? $key.':'.$parent : $key.':null';
        }
    }
}
# Final array store (saves the rows)
$store  =   [];
# Loop rows
foreach($arr as $key => $row) {
    # Reset the $new array on every iteration so you don't just
    # overwrite the same array
    $new    =   [];
    # Recurse
    recParent($row,$new);
    # Store the current row and implode the key/values
    $store[$key]    =   implode(PHP_EOL,$new);
}

print_r($store);

应该给你:

Array
(
    [0] => id:null
publisherId:null
photos:null
attributes:null
width:attributes
height:attributes
info:null
val1:info
val2:info
location:info
address:location
country:address
destination:location
country:destination
online:null
    [1] => id:null
publisherId:null
photos:null
attributes:null
width:attributes
height:attributes
info:null
val1:info
val2:info
location:info
address:location
country:address
destination:location
country:destination
online:null
)

假设所有数组都是相同的结构,只需使用第一个数组:

$new    =   [];
# Recurse once
recParent($arr[0],$new);
# Implode that one array
$final = implode(PHP_EOL,$new);
print_r($final);

那只会给你:

id:null
publisherId:null
photos:null
attributes:null
width:attributes
height:attributes
info:null
val1:info
val2:info
location:info
address:location
country:address
destination:location
country:destination
online:null