简体   繁体   中英

Looping through a SimpleXML with foreach in php

I'm trying to get the array values into the next SimpleXMLElement Object to get the [property] values, but I am not sure what is the best way to loop this object with an foreach in PHP,

Object code that I am getting:

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
                        )

                )

        )

)

Could you please help with the best way to looping inside this object?

Thanks!

$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);

preview it here: 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
            .....

here is an example:

advantage is in one line, I don't think you'll find better:)

return 100% array not object of XMLElement

example for xml which does not contain DATA,

for xml with CDATA see: example XML with CDATA to array

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

assuming the XML file looks like this:

<?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>

if you want to dump

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

otherwise please give the example xml file !

An example XML conversion with CDATA to array

without indexing:

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

result

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"
  ]
]

with indexation:

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()))

result

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 file

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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