Page 1 of 1
Native PHP array function(s)?
Posted: Sun Jul 29, 2007 5:22 pm
by alex.barylski
I have the following:
Code: Select all
GLOBALS['error'][] = array($line, $file, $code, $info);
I want to quickly dump this multidimensional array to file...how can I use PHP function(s) to quickly and easily convert the above kind of array into a multi-line string for a dump to the HDD.
Any ideas or am I going to have to hand code something quick and dirty?
Posted: Sun Jul 29, 2007 6:40 pm
by John Cartwright
Sorry, what exactly are you asking?
Posted: Sun Jul 29, 2007 6:41 pm
by vigge89
What do you wish the output to look like?
For just generating a raw unformatted string there's always
which you can use together with your method of choice for saving the string to a file. I haven't encountered any functions which dumps the contents of a variable directly to a file though.
Posted: Sun Jul 29, 2007 8:51 pm
by alex.barylski
I'm looking for some quick one or two PHP array function calls to basically take an array of the form:
Code: Select all
$GLOBALS['errors'] = array(
array(1, 2, 43434, "An error occured"),
array(1, 2, 43434, "An error occured"),
array(1, 2, 43434, "An error occured"),
array(1, 2, 43434, "An error occured"),
);
Convert it into a string which can be serialized in a CSV fashion.
Code: Select all
1, 2, 43434, An error occured
1, 2, 43434, An error occured
1, 2, 43434, An error occured
1, 2, 43434, An error occured
Easy enough to implement but ideally I could use a shortcut using one or array_* calls. I've looked over the docs but I can't figure out any combination so I've just iterate the array and format.
Posted: Sun Jul 29, 2007 9:09 pm
by volka
Take a look at
http://de2.php.net/var_export
Code: Select all
file_put_contents('data.php', '<?php return ' . var_export($GLOBALS['errors'], true) . ';');
...
$errors = require 'data.php';
Posted: Mon Jul 30, 2007 3:31 am
by stereofrog
something like this maybe?
Code: Select all
$result = array_map('implode',
array_fill(0, count($ary), ','),
$ary);
Posted: Mon Jul 30, 2007 4:57 am
by onion2k
I handle errors using a registry object (I think, I'm not very good at 'Name that pattern!'). It maintains an array internally for debugging information. Every time an error is generated it saves the text error message, the microtime value of when it happened, and a few other things. There's an instance of it within my controller object, accessible by everything the controller does (which is pretty much everything in the site). Obviously as it's an object I can extend it with methods for all sorts of output, dumping to a file, outputting as HTML, saving to a database table, whatever. Putting things like debugging info into the $_GLOBALS array seems to be a bit of a hack to me.