phpdrupalphp-5.3watchdog

Drupal Watchdog script generating Notice: Array to string conversion


We're running PHP 5.3.10-1ubuntu3.15 with Suhosin-Patch, and I just ran across the weirdest thing. I keep getting an Array to string conversion error.

Here is some code with line numbers:

115 $report['report'][$key]['report'] = array();
116 watchdog('ranking_report_field', 'key is a: ' . gettype($key), array(), WATCHDOG_NOTICE);
117 $report['report'][$key]['report'] = array(
    '#markup' => "<p>No information available.</p><p>For questions, <a href='mailto:$emailAddr'>email</a> your account executive ($emailAddr).</p>",
);

Here are Drupal's (sequential) logs for those line numbers:

Notice: Array to string conversion in foo() (line 115 of /var/www/...
key is a: string
Notice: Array to string conversion in foo() (line 117 of /var/www/...

So far as I can tell there's no array to string conversion that should be taking place. Someone help me out with a second pair of eyes, please - or is this some kind of bug that just hit PHP?


Solution

  • One of the array keys is mapped to a string not an array. Here is a program for how such an error could occur.

    <?php
    
    $key = 0;
    
    $report = array();
    $report['report'] = array();
    $report['report'][$key] = 'report';
    
    // Array to string conversion error
    $report['report'][$key]['report'] = array();
    
    // what I assume you are expecting is
    $report['report'][$key] = array();
    $report['report'][$key]['report'] = array(); // no more notices
    

    NOTE: at his time the OP has not included info for how the array is created