简体   繁体   English

PHP中的语法错误:函数返回值的数组访问

[英]Syntax error in PHP: array access of a function's return value

Well the problem is that I wrote a code that's avoiding showing of duplicate data. 问题是我写了一个避免显示重复数据的代码。 On my local machine it works perfectly but on host I am getting a following error: 在我的本地计算机上,它工作正常,但是在主机上,我收到以下错误:

Syntax error, unexpected '[' in /home/eplus/public_html/vqmod/vqcache/vq2-catalog_view_theme_default_template_product_product.tpl on line 481 语法错误,在第481行的/home/eplus/public_html/vqmod/vqcache/vq2-catalog_view_theme_default_template_product_product.tpl中出现意外的[[]

Here is the code where error occurs 这是发生错误的代码

if ($pr_id[$i] == 0) {
   break;
   echo 'h1' . "Нет похожих продуктов";
}

if ($pr_id[$i] != array_unique($pr_id)[$i]) {    // Error on this line
   $product_fee = $this->db->query("SELECT `product_id` FROM `" . DB_PREFIX . "product_to_category` WHERE `category_id`='".$feed_id."' AND NOT `product_id` = '".$products_id."'  GROUP BY `product_id` ORDER BY RAND() LIMIT 0,10");

   $pr_id[$i] = $product_fee->row['product_id'];

   continue;
}

How can I avoid this? 如何避免这种情况? As for CMS I am currently using OpenCart. 至于CMS,我目前正在使用OpenCart。

Array dereferencing is supported only in PHP version 5.4 and above. 数组解引用仅在PHP 5.4及更高版本中受支持。
It is when you use bracket access directly after a function that returns an array: array_unique($array)[0] . 当您在返回数组的函数之后直接使用方括号访问时: array_unique($array)[0]

Read more: PHP 5.4: New features 阅读更多: PHP 5.4:新功能

I would advise you to create the array with unique items before the if clause: 我建议您在if子句之前创建具有唯一项的数组:

$pr_unique = array_unique($pr_id);

if ($pr_id[$i] != $pr_unique[$i]) {
    ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM