Fix Notice: Undefined Offset: 0 in Magento 2

magento2

Exception #0 (Exception): Notice: Undefined offset: 0 in /var/www/clients/client0/web2/web/app/code/Mirasvit/Search/Block/Result.php

 * @return IndexInterface
 */
public function getFirstMatchedIndex()
{
    foreach ($this->getIndices() as $index) {
        if (($index->getData('store_id') == false
            || $index->getData('store_id') == $this->getCurrentStore()->getId())
        ) {
            return $index;
        }
    }

    return $this->getIndices()[0];
}

Best Answer

use this code instead

* @return IndexInterface
 */
public function getFirstMatchedIndex()
{
    foreach ($this->getIndices() as $index) {
        if (($index->getData('store_id') == false
            || $index->getData('store_id') == $this->getCurrentStore()->getId())
        ) {
            return $index;
        }
    }

    if(count($this->getIndices()) > 0){
        return $this->getIndices()[0];
    }
    return array();
}

This should work.

Related Topic