dspym82000 2012-03-13 15:28 采纳率: 100%
浏览 67

PHP - 访问SimpleXML :: xpath函数数组的第一个索引

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];

This works fine.

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.

  • 写回答

3条回答 默认 最新

  • dongman2721 2012-03-13 15:31
    关注

    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];
    

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

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

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度