Php 从简单xml数据设置数组
我正在尝试使用以下代码从xml提要创建标题数组:Php 从简单xml数据设置数组,php,xml,Php,Xml,我正在尝试使用以下代码从xml提要创建标题数组: $url = 'https://indiegamestand.com/store/salefeed.php'; $xml = simplexml_load_string(file_get_contents($url)); $on_sale = array(); foreach ($xml->channel->item as $game) { echo $game->{'title'} . "\n"; $on_
$url = 'https://indiegamestand.com/store/salefeed.php';
$xml = simplexml_load_string(file_get_contents($url));
$on_sale = array();
foreach ($xml->channel->item as $game)
{
echo $game->{'title'} . "\n";
$on_sale[] = $game->{'title'};
}
print_r($on_sale);
echo$game->{'title'}。“\n”;返回正确的标题,但在将标题设置为数组时,我会收到以下垃圾邮件:
Array
(
[0] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[1] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[2] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
设置此阵列时是否遗漏了某些内容?请使用以下方法:
$on_sale[]=$game->{'title'}->\uu toString()代码>
或者更好(在我看来):
$on_sale[]=(字符串)$game->{'title'}代码>
当您将对象添加到数组中时,PHP不知道您需要字符串值,因此\uuu toString()
不会像在echo
调用中那样自动调用。将对象强制转换为string
时,将自动调用\uuuu toString()
仅供参考:你也不需要花括号,这对我来说很好:
$on_sale[]=(字符串)$game->title代码>