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.
If you want to encode/decode an array like this, you should either use
var_export()
, which generates output in PHP's array for, for instance:could be the result of it. You would have to use
eval()
to get the array back, though, and that is a potentially dangerous way (especially sinceeval()
really executes PHP code, so a simple code injection could make hackers able to gain control over your PHP script).Some even better solutions are
serialize()
, which creates a serialized version of any array or object; andjson_encode()
, which encodes any array or object with the JSON format (which is more preferred for data exchange between different languages).var_export
orserialize
is what you're looking for.var_export
will render a PHP parsable array syntax, andserialize
will 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:
I think you are looking for the
serialize
function:It allows you to save the contents of array in readable format and later you can read the array back with
unserialize
function.Using these functions, you can store/retrieve the arrays even in text/flat files as well as database.
Updated to NOT USE create_function, as it is DEPRECATED as of PHP 7.2.0. Instead it is replaced to use anonymous functions:
Use regexp to change array(.) { (.*) } to array($1) and eval the code, this is not so easy as written because You have to deal with matching brackets etc., just a clue on how to find solution ;)
There's no other way than manual parsing depending on the type. I didn't add support for objects, but it's very similar to the arrays one; you just need to do some reflection magic to populate not only public properties and to not trigger the constructor.
EDIT: Added support for objects... Reflection magic...
(Here are a lot of "magic" numbers when incrementing string position counter
$i
, mostly just string lengths of the keywords and some parenthesis etc.)