Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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 从简单xml数据设置数组_Php_Xml - Fatal编程技术网

Php 从简单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_

我正在尝试使用以下代码从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_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