PHP foreach won't work

What am I doing wrong here?

<?php
 $xml=simplexml_load_file("<dict>
 <key>ChannelID</key>
 <string>example</string>
 <key>ChannelDVRs</key>
 <array>
 <dict>
 <key>id</key>
 <string>LIVE</string>
 <key>name</key>
 <string>example1111</string>
 <key>description</key>
 <string>Live</string>
 <key>logoUrl</key>
 <string>http://www.example.com/logos/190x110/298.jpg</string>
 <key>logoUrlSD</key>
 <string>http://www.example.com/logos/190x110/298.jpg</string>
 <key>isVOD</key>
 <string>false</string>
 <key>StreamURL</key>
 <string>exampleessssssssssss test</string>
 </dict>
 </array>
 </dict>") 
 or die("Error: Cannot create object");

foreach ($xml->string as $output) {
echo "$output <br>";
}?>

If there is some way to get the last string which says exampleessssssssssss test I don't need to use foreach. THANK for any help ;)

3 answers

  • answered 2018-03-13 21:53 Felippe Duarte

    You need to load the XML string properly, then fetch the right element.

    $xml = simplexml_load_string(
        "<dict>
            <key>ChannelID</key>
            <string>example</string>
            <key>ChannelDVRs</key>
            <array>
                <dict>
                    <key>id</key>
                    <string>LIVE</string>
                    <key>name</key>
                    <string>example1111</string>
                    <key>description</key>
                    <string>Live</string>
                    <key>logoUrl</key>
                    <string>http://www.example.com/logos/190x110/298.jpg</string>
                    <key>logoUrlSD</key>
                    <string>http://www.example.com/logos/190x110/298.jpg</string>
                    <key>isVOD</key>
                    <string>false</string>
                    <key>StreamURL</key>
                    <string>exampleessssssssssss test</string>
                </dict>
            </array>
        </dict>") 
     or die("Error: Cannot create object");
    
    echo end($xml->array->dict->string); //output exampleessssssssssss test
    

    $xml->array->dict->string is an array, so the end() function will get the last element, as you seems to want.

  • answered 2018-03-13 22:01 Nigel Ren

    Just an alternative, which doesn't rely on the content being in any order other than being the element following <key>StreamURL</key>. This ensures that if there are future changes, the code doesn't stop working.

    $xml=simplexml_load_string("<dict>
     <key>ChannelID</key>
     <string>example</string>
     <key>ChannelDVRs</key>
     <array>
     <dict>
     <key>id</key>
     <string>LIVE</string>
     <key>name</key>
     <string>example1111</string>
     <key>description</key>
     <string>Live</string>
     <key>logoUrl</key>
     <string>http://www.example.com/logos/190x110/298.jpg</string>
     <key>logoUrlSD</key>
     <string>http://www.example.com/logos/190x110/298.jpg</string>
     <key>isVOD</key>
     <string>false</string>
     <key>StreamURL</key>
     <string>exampleessssssssssss test</string>
     </dict>
     </array>
     </dict>")
     or die("Error: Cannot create object");
    
    $text = $xml->xpath('//key[text()="StreamURL"]/following-sibling::string/text()');
    echo (string)$text[0];
    

    Using XPath allows you to search based on an elements content and then this picks up the following <string> element. As xpath() returns a list of matching nodes, use [0] to get the first item.

  • answered 2018-03-14 01:44 Seif Hatem

    altering @Felippe Duarte code to echo with str_replace use the below code

    $str =  end($xml->array->dict->string);
    $edited_str = str_replace("exampleessssssssssss","examples",$str);
    echo $edited_str;