Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP foreach赢得';行不通_Php_Xml_Loops_Foreach - Fatal编程技术网

PHP foreach赢得';行不通

PHP foreach赢得';行不通,php,xml,loops,foreach,Php,Xml,Loops,Foreach,我做错了什么 <?php $xml=simplexml_load_file("<dict> <key>ChannelID</key> <string>example</string> <key>ChannelDVRs</key> <array> <dict> <key>id</key> <string>LIVE</string

我做错了什么

<?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>";
}?>


如果有某种方法可以得到最后一个字符串,它是
examplesssss test
,我不需要使用foreach。谢谢你的帮助;)

您需要正确加载XML字符串,然后获取正确的元素

$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=simplexml\u加载\u字符串(
"
渠道
实例
频道录像机
身份证件
居住
名称
例1111
描述
居住
logoUrl
http://www.example.com/logos/190x110/298.jpg
logoUrlSD
http://www.example.com/logos/190x110/298.jpg
isVOD
错误的
StreamURL
示例SSSS测试
") 
或死亡(“错误:无法创建对象”);
回显结束($xml->array->dict->string)//输出示例SSSS测试

$xml->array->dict->string
是一个数组,因此
end()
函数将获得最后一个元素,正如您所希望的那样。

只是另一种选择,它不依赖于除
StreamURL
之后的元素以外的任何顺序的内容。这确保了如果将来有更改,代码不会停止工作

$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];
$xml=simplexml\u load\u字符串(“
渠道
实例
频道录像机
身份证件
居住
名称
例1111
描述
居住
logoUrl
http://www.example.com/logos/190x110/298.jpg
logoUrlSD
http://www.example.com/logos/190x110/298.jpg
isVOD
错误的
StreamURL
示例SSSS测试
")
或死亡(“错误:无法创建对象”);
$text=$xml->xpath('//key[text()=“StreamURL”]/以下同级::string/text());
echo(字符串)$text[0];

使用XPath可以基于元素内容进行搜索,然后选择以下
元素。由于
xpath()
返回匹配节点的列表,请使用
[0]
获取第一项。

更改@Felippe Duarte代码以使用str\u replace进行回显,请使用以下代码

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

查看
加载\u文件
。。。或者是url链接,所以我只得到一个示例哦,这不是你的实际代码。我在这里做错了什么?你应该描述一下当前发生的事情。问题真的是
我不需要使用foreach?
,下面用
end
回答了这个问题。现在我回音时如何使用stru\u replace函数?我会这样做:)它说:你可以在4分钟内接受答案