Array to string conversion error in Magento logs

My Magento system.log is full with this error: Notice: Array to string conversion in /app/code/core/Mage/Core/Block/Abstract.php on line 1232.

I've added the code here: (rule 1232 is $key = implode('|', $key);)

   /**
      * Get Key for caching block content
      *
      * @return string
      */
     public function getCacheKey()
     {
         if ($this->hasData('cache_key')) {
             return $this->getData('cache_key');
         }
         /**
         * don't prevent recalculation by saving generated cache key
          * because of ability to render single block instance with different data
          */
         $key = $this->getCacheKeyInfo();
         //ksort($key);  // ignore order
         $key = array_values($key);  // ignore array keys
         $key = implode('|', $key);
         $key = sha1($key);
         return $key;
    }

Do you know what's going wrong, and what I need to change? Thank you in advance!


Well this is magento core code so i do expect some wrong value getting into $key variable from outside of this code.

You can try to log the $key variable and check if it is an array.

Something like:

Mage::log(print_r($key,true), null, 'mycustomerror.log', true);

or

Mage::log($key, null, 'mycustomerror.log', true);

If you want a better answer you should tell us when this notice is generated and the modules you are using.

After extra info

Well it is a guess because the information you gave me is not much. But your logs shows the following:

2013-01-17T16:52:36+00:00 DEBUG (7): Array
(
    [0] => CATALOG_PRODUCT_HIGHLIGHT
    [1] => 1
    [2] => default
    [3] => a058
    [4] => 0
    [5] => highlight/product/grid.phtml
    [6] => 8
    [7] => 4
    [8] => Array
        (
        )

    [9] => Array
        (
        )

    [10] => Array
        (
        )

    [11] => Aanbiedingen
    [12] => highlight-special
    [13] => special_from_date,special_to_date
    [14] => -special
    [15] => product_special
)

I think the problem is:

[8] => Array()
[9] => Array()
[10] => Array()

When they are imploded they will give the notice.

SEE: http://codepad.viper-7.com/qb5tIW

I would check the CATALOG_PRODUCT_HIGHLIGHT (highlight/product/grid.phtml)

You can navigate your site so that the highlight/product/grid.phtml is loaded and see if this call is indeed putting that notice in your log files.

good luck !


After noticing one visit to my site's homepage made 90+ lines of this warning in system.log , I tackled the problem of nested arrays inferred in khoekman's answer. My Magento version is 1.7.0.1

Step 1
Create the path: app/code/local/Mage/Core/Block and then cd to that directory.

Step 2
Copy Abstract.php from core to local:
cp ~/public_html/app/code/core/Mage/Core/Block/Abstract.php .
This is how you "override the core file" for your custom edits.

Step 3
Open the new file ~/public_html/app/code/local/Mage/Core/Block/Abstract.php in your favorite editor, scroll down to line 1232:

Change from this:
$key = implode('|', $key );
to:
$key = $this->implodeRecursive('|', $key );

Step 4
Scroll above this current function public function getCacheKey() and then in the free space above it, add in the recursive implode function:

//  ckck: fix of system.log Notice: Array to string conversion in app/code/core/Mage/Core/Block/Abstract.php on line 1232
private function implodeRecursive( $_glue, $_arrIn ) {
    $_arrTmp=Array();
    if( count( $_arrIn ) > 1 ) {
        foreach( $_arrIn as $key => $value ) {
            if( is_string($value) || is_null($value) )
                $_arrTmp[] = $value;
            else
                $_arrTmp[] = $this->implodeRecursive( $_glue, $value );
        }
    }
    $_strReturn = implode( $_glue, $_arrTmp );
    return $_strReturn;
}

Through trial and error, the only reliable test was if $value is a string. My usual tricks of is_array() or testing for count() > 0 did not work. Fixing this bug was an interesting endeavor.

Step 5
Save then close Abstract.php and then check in the new file into your version control system.
I use git with merge --no-ff branching.

链接地址: http://www.djcxy.com/p/45068.html

上一篇: 如何检查核心文件是否已被修改或不在magento中?

下一篇: 数组到字符串转换错误在Magento日志中