PHP-访问SimpleXML :: xpath函数数组的第一个索引 [英] PHP - Access first index of array of SimpleXML::xpath function

查看:44
本文介绍了PHP-访问SimpleXML :: xpath函数数组的第一个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SimpleXML来获取由标签名称和属性指定的XML对象的元素,如下所示:

I am using SimpleXML to get an element of an XML object specified by tag name and attribute... as follows:

$result = $xml->xpath('Stat[@Type="Venue"]');
$venue  = $result[0];

这很好.

但是... 以下缩短给了我一个错误

However... the following shortening gives me an error

$venue = $xml->xpath('Stat[@Type="Venue"]')[0];

PHP Parse error:  syntax error, unexpected '[' in /var/www/handler.php on line 10

我必须变得愚蠢…….但是我似乎无法弄清这一点.

I've got to be being stupid.... but I cannot seem to figure this out.

推荐答案

您不能使用这样的数组.您需要将其传递给像这样的变量

You can't use an array like that. You need to pass it to a variable like so

$venue = $xml->xpath('Stat[@Type="Venue"]');
echo $venue[0];

我认为在PHP 5.4中,您将能够从对象访问数组,但是请不要在其上引用我.

I think in PHP 5.4 you will have the ability to access array from objects, but don't quote me on that.

抱歉,我从OP复制并粘贴了代码. [0] 从我的雷达上滑了下来!

Sorry about that, I copied and pasted the code from the OP. [0] slipped from my radar!

这篇关于PHP-访问SimpleXML :: xpath函数数组的第一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆