PHP:xpath由于意外的"["而无法在php5.3上运行

mrk*_*rks 1 php xpath php-5.3

我在PHP5.4上运行以下行没有任何问题:

$lstContent = $data->xpath("/response/lst[@name='highlighting']")[0]->lst[$i]->arr->str;
Run Code Online (Sandbox Code Playgroud)

但是现在在PHP5.3(生产系统)上我收到以下错误:

解析错误:语法错误,第153行的/var/www/html/upload/inc_suche_code.php中的意外'['

有什么想法快速解决?更新PHP对我不起作用.

Opt*_*ime 6

在旧版本的PHP中,您无法直接访问作为函数结果的变量的数组值.您必须使用临时变量拆分表达式.

$result = $data->xpath("/response/lst[@name='highlighting']");
$lstContent = $result[0]->lst[$i]->arr->str;
Run Code Online (Sandbox Code Playgroud)

从PHP 5.4开始,可以直接对函数或方法调用的结果进行数组解除引用.之前只能使用临时变量.

资料来源:http://php.net/manual/en/language.types.array.php

编辑:必须"你还应该考虑升级你的PHP版本".这个恼人的限制在很久以前就已经确定了,更不用说5.3 在2014年已经结束了生命,这意味着它自那时起就没有得到安全升级.