gpt4 book ai didi

php - simplexml_load_string foreach 不显示任何内容

转载 作者:行者123 更新时间:2023-12-04 05:04:30 26 4
gpt4 key购买 nike

我的 PHP 代码如下所示,显示空白 我想显示所有带有超链接的记录,例如

<a href="test.php?id=$Id">$Name</a>

其中 $Id, $Name 来自 XML 提要
  $path = 'www.abc.com/test.xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned = curl_exec($ch);
curl_close($ch);
// $xml === False on failure
$xml = simplexml_load_string($returned);

foreach( $xml->Name as $Name){
print (string)$Name;
}

我的 XML 如下所示
<ABCXml version="1.1.0">
<Manufacturers>
<Item>
<Id>219</Id>
<Name>Matrix Corp</Name>
</Item>
<Item>
<Id>2040</Id>
<Name>Microcomputer</Name>
</Item>
</Manufacturers>
</ABCXml>

最佳答案

您的 xml 对象以制造商开头,其中包含多个包含名称和 ID 的项目。你需要找到他们。

foreach($xml->Manufacturers->Item as $item) {
printf("<a href='test.php?id=%s>%s</a>", $item->Id, $item->Name);
}

关于php - simplexml_load_string foreach 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15712483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com