phparraysarrayaccess

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

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.


Solution

  • Array dereferencing is supported only in PHP version 5.4 and above.
    It is when you use bracket access directly after a function that returns an array: array_unique($array)[0].

    Read more: PHP 5.4: New features

    I would advise you to create the array with unique items before the if clause:

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