Welcome Guest, Not a member yet? Register   Sign In
Log an Array


I'm constantly in need for a way to debug and log some Arrays.

I'd like to log it in the log file instead of having to use a var_dump.

I've added the following function to the Commun.php file of code_igniter, it works but it's probably ugly. If someone knows of a better way i'm interested ...

function log_array($level = 'error', $array, $php_error = FALSE)
    static $LOG;
    $config =& get_config();
    if ($config['log_threshold'] == 0)

    $LOG =& load_class('Log');    
    $LOG->write_log($level, $tab_debug, $php_error);


[eluser]Paul Scott[/eluser]
You can use var_export to simply return the structure of the array as a string, instead of using the output buffer, and then you could log that. Similarly with print_r.
So you could do something like
$my_array = array (
  0 => 1,
  1 => 2,
  2 => array (
    0 => 'a',
    1 => 'b',
    2 => 'c',

$log_my_error = var_export($my_array, TRUE); // Or alternatively, print_r($my_array, TRUE);

// Then you might want to put it onto one line
$log_my_error = str_replace(array("\r","\n"), '', $log_my_Error);
And now you'd have `$log_my_error` - a string containing the structure of your array.

Theme © iAndrew 2016 - Forum software by © MyBB