首页 > 解决方案 > 在php中使用foreach循环遍历SimpleXML

问题描述

我正在尝试将数组值放入下一个 SimpleXMLElement 对象以获取 [property] 值,但我不确定在 PHP 中使用 foreach 循环该对象的最佳方法是什么,

我得到的目标代码:

SimpleXMLElement Object
(
    [nowplaying-info] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [mountName] => KDLDFMAAC
                            [timestamp] => 1621880102
                            [type] => track
                        )

                    [property] => Array
                        (
                            [0] => 248000
                            [1] => 1621880102606
                            [2] =>    LOS ANGELES AZULES FT PEPE AGILAR
                            [3] => 97008
                            [4] => NI CONTIGO NI SIN TI
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [mountName] => KDLDFMAAC
                            [timestamp] => 1621879804
                            [type] => track
                        )

                    [property] => Array
                        (
                            [0] => 185000
                            [1] => 1621879804060
                            [2] => 
 
FITO OLIVARES
                            [3] => 97754
                            [4] => JUANA LA CUBANA
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [mountName] => KDLDFMAAC
                            [timestamp] => 1621879192
                            [type] => track
                        )

                    [property] => Array
                        (
                            [0] => 252000
                            [1] => 1621879192436
                            [2] =>    CHON ARAUZA
                            [3] => 97076
                            [4] => UN MONTON DE ESTRELLAS
                        )

                )

        )

)

您能否提供在此对象内循环的最佳方法?

谢谢!

标签: phpsimplexmlstdclass

解决方案


$xml = simplexml_load_file('https://np.tritondigital.com/public/nowplaying?mountName=KFRQFMAAC&numberToFetch=3&eventType=track&request.preventCache=1621376522745');
$tracks = [];
foreach($xml->{'nowplaying-info'} as $item)
{
     $properties = [];
     foreach ($item->property as $prop)  $properties[] = (String) $prop;
     $tracks[]=$properties;
}

print_r($tracks);

在这里预览:https ://www.tehplayground.com/8zX2DrFsjVjT1IUK


// output: 
Array
(
    [0] => Array
        (
            [0] => 03:07
            [1] => 1622057699278
            [2] => LIVE AND LET DIE
            [3] => 18281
            [4] => PAUL MCCARTNEY & WINGS
        )

    [1] => Array
        (
            [0] => 04:20
            [1] => 1622057449584
            [2] => DREAMS
            [3] => 18269
            [4] => FLEETWOOD MAC
        )

    [2] => Array
        (
            [0] => 05:03
            [1] => 1622057144393
            .....

推荐阅读