在 php 中使用 foreach 循环遍历 SimpleXML

Looping through a SimpleXML with foreach in php

我正在尝试将数组值放入下一个 SimpleXMLElement 对象以获取 [属性] 值,但我不确定在 PHP,

我得到的目标代码:

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
                        )

                )

        )

)

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

谢谢!

$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
            .....

这里有一个例子:

优势在于一条线,我不认为你会找到更好的:)

return 100% 数组不是 XMLElement

的对象

xml 的示例不包含数据,

对于使用 CDATA 的 xml,请参阅:

array_map(function ($param) {
                    return next($param);
                }, current((new SimpleXMLElement(file_get_contents('newXMLDocument.xml')))->children()))

假设 XML 文件如下所示:

<?xml version="1.0" standalone="yes"?>
<nowplaying-info-list>
    <nowplaying-info mountName="KDLDFMAAC" timestamp="1621880102" type="track">
        <property>248000</property>
        <property>1621880102606</property>
        <property>LOS ANGELES AZULES FT PEPE AGILAR</property>
        <property>97008</property>
        <property>NI CONTIGO NI SIN TI</property>
    </nowplaying-info>
    <nowplaying-info mountName="KDLDFMAAC" timestamp="1621879804" type="track">
        <property>185000</property>
        <property>1621879804060</property>
        <property>FITO OLIVARES</property>
        <property>97754</property>
        <property>JUANA LA CUBANA</property>
    </nowplaying-info>
    <nowplaying-info mountName="KDLDFMAAC" timestamp="1621879192" type="track">
        <property>252000</property>
        <property>1621879192436</property>
        <property>CHON ARAUZA</property>
        <property>97076</property>
        <property>UN MONTON DE ESTRELLAS</property>
    </nowplaying-info>
</nowplaying-info-list>

如果你想转储

echo nl2br(print_r(array_map(function ($param) {
                    return next($param);
                }, current((new SimpleXMLElement(file_get_contents('newXMLDocument.xml')))->children())), true));

否则请给出例子xml文件!

使用 CDATA 到数组的 XML 转换示例

没有索引:

    array_map(function ($nowplaying) {
        return array_map(function ($property) {
    return (string) $property;
}, next($nowplaying));
    }, current((new SimpleXMLElement(file_get_contents('nowplaying.xml')))->children()))

结果

array:3 [▼
  0 => array:5 [▼
    0 => "05:05"
    1 => "1622016640460"
    2 => "YOU'VE GOT ANOTHER THING COMIN'"
    3 => "23008"
    4 => "JUDAS PRIEST"
  ]
  1 => array:5 [▼
    0 => "04:22"
    1 => "1622016380462"
    2 => "DREAM ON"
    3 => "20354"
    4 => "AEROSMITH"
  ]
  2 => array:5 [▼
    0 => "03:54"
    1 => "1622016139890"
    2 => "ALL ALONG THE WATCHTOWER"
    3 => "20112"
    4 => "HENDRIX, JIMI"
  ]
]

带索引:

array_map(function ($nowplaying) {
    return array_combine(array_map(function ($property) {
        return current($property)['name'];
    }, next($nowplaying)), array_map(function ($property) {
        return (string) $property;
    }, next($nowplaying)));
}, current((new SimpleXMLElement(file_get_contents('nowplaying.xml')))->children()))

结果

array:3 [▼
  0 => array:5 [▼
    "cue_time_duration" => "05:05"
    "cue_time_start" => "1622016640460"
    "cue_title" => "YOU'VE GOT ANOTHER THING COMIN'"
    "program_id" => "23008"
    "track_artist_name" => "JUDAS PRIEST"
  ]
  1 => array:5 [▼
    "cue_time_duration" => "04:22"
    "cue_time_start" => "1622016380462"
    "cue_title" => "DREAM ON"
    "program_id" => "20354"
    "track_artist_name" => "AEROSMITH"
  ]
  2 => array:5 [▼
    "cue_time_duration" => "03:54"
    "cue_time_start" => "1622016139890"
    "cue_title" => "ALL ALONG THE WATCHTOWER"
    "program_id" => "20112"
    "track_artist_name" => "HENDRIX, JIMI"
  ]
]

XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<nowplaying-info-list>
    <nowplaying-info mountName="KFRQFMAAC" timestamp="1622016640" type="track">
        <property name="cue_time_duration"><![CDATA[05:05]]></property>
        <property name="cue_time_start"><![CDATA[1622016640460]]></property>
        <property name="cue_title"><![CDATA[YOU'VE GOT ANOTHER THING COMIN']]></property>
        <property name="program_id"><![CDATA[23008]]></property>
        <property name="track_artist_name"><![CDATA[JUDAS PRIEST]]></property>
    </nowplaying-info>
    <nowplaying-info mountName="KFRQFMAAC" timestamp="1622016380" type="track">
        <property name="cue_time_duration"><![CDATA[04:22]]></property>
        <property name="cue_time_start"><![CDATA[1622016380462]]></property>
        <property name="cue_title"><![CDATA[DREAM ON]]></property>
        <property name="program_id"><![CDATA[20354]]></property>
        <property name="track_artist_name"><![CDATA[AEROSMITH]]></property>
    </nowplaying-info>
    <nowplaying-info mountName="KFRQFMAAC" timestamp="1622016139" type="track">
        <property name="cue_time_duration"><![CDATA[03:54]]></property>
        <property name="cue_time_start"><![CDATA[1622016139890]]></property>
        <property name="cue_title"><![CDATA[ALL ALONG THE WATCHTOWER]]></property>
        <property name="program_id"><![CDATA[20112]]></property>
        <property name="track_artist_name"><![CDATA[HENDRIX, JIMI]]></property>
    </nowplaying-info>
</nowplaying-info-list>