繁体   English   中英

PHP在嵌套数组/对象内获取值

[英]PHP get value inside nested array/object

我正在尝试在此对象(xml)中返回typeName键的值。

[geometricProperty] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [typeName] => localizacao  // < - - - This is the value I need.
                    )

                [Point] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [ID] => swrefVgetZredeVcollX757678785
                                [srs] => ut_cm
                            )

                        [coordinates] => 38871,739716 
                    )

            )

        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [typeName] => limite
                    )

                [Polygon] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [ID] => swrefVgeometryV08945038
                                [srs] => utm_23_cm
                            )

                        [outerBoundaryIs] => SimpleXMLElement Object
                            (
                                [LinearRing] => SimpleXMLElement Object
                                    (
                                        [coordinates] => 318950,7399585 31981,39650 31826,73990 316956,79750 
                                    )

                            )

                    )

            )

...

我尝试了以下操作,但返回NULL

echo "<p>This object has ".(count($n->Feature->geometricProperty))." items.</p>";
$c=0;
foreach($n->Feature->geometricProperty as $k){
    $c++;
    echo "<p>$c)".(gettype($k))."</p>";

    foreach(array_keys(get_object_vars($k)) as $o){ 
        echo "<p>$o</p>";

        switch($o){
            case "@attributes": var_dump($k->{$o}["typeName"]); $tipo=$k->{$o}["typeName"]; break;
            case "Point": $value=$k->{$o}->coordinates; break;
            case "Polygon": $value=$k->{$o}->outerBoundaryIs->LinearRing->coordinates; break;
            case "Line...": break;
        }
    }

    echo "<p>TIPO: $tipo</p><p>VALOR: $value</p>";

}

OUTPUT:

This object has 3 items.

1)object

@attributes
NULL

Point

TIPO:

VALOR: 31869871,739796106

2)object

@attributes
NULL

Polygon

TIPO: 

搜索并发现此溢出问题 ,无法猜测我在做什么错。

您可以使用SimpleXmlElement::attributes()获取属性数组。 因此,您就可以在它们上循环并显示它们,或者使用implode或其他方式...取决于您想要的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM