Native PHP array function(s)?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Native PHP array function(s)?

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Sorry, what exactly are you asking?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

What do you wish the output to look like?
For just generating a raw unformatted string there's always

Code: Select all

$dump = print_r($array, true);
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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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';
Last edited by volka on Mon Jul 30, 2007 3:43 am, edited 2 times in total.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

something like this maybe?

Code: Select all

$result = array_map('implode', 
	array_fill(0, count($ary), ','), 
	$ary);
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Post Reply