I have never really thought about this until today, but after searching the web I didn’t really find anything. Maybe I wasn’t wording it right in the search.
Given an array (of multiple dimensions or not):
$data = array('this' => array('is' => 'the'), 'challenge' => array('for' => array('you')));
When var_dumped:
array(2) { ["this"]=> array(1) { ["is"]=> string(3) "the" } ["challenge"]=> array(1) { ["for"]=> array(1) { [0]=> string(3) "you" } } }
The challenge is this: What is the best optimized method for recompiling the array to a useable array for PHP? Like an undump_var() function. Whether the data is all on one line as output in a browser or whether it contains the line breaks as output to terminal.
Is it just a matter of regex? Or is there some other way? I am looking for creativity.
UPDATE: Note. I am familiar with serialize and unserialize folks. I am not looking for alternative solutions. This is a code challenge to see if it can be done in an optimized and creative way. So serialize and var_export are not solutions here. Nor are they the best answers.
var_exportorserializeis what you’re looking for.var_exportwill render a PHP parsable array syntax, andserializewill render a non-human readable but reversible “array to string” conversion…Edit Alright, for the challenge:
Basically, I convert the output into a serialized string (and then unserialize it). I don’t claim this to be perfect, but it appears to work on some pretty complex structures that I’ve tried…
I tested it on a complex structure such as: