phpsimplexmlstdclass

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!


Solution

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