How to create an array from output of var_dump in PHP?
Asked Answered
P

6

6

How can I parse the output of var_dump in PHP to create an array?

Pantechnicon answered 15/9, 2009 at 8:39 Comment(1)
Does this answer your question? Convert var_dump of array back to array variableDenier
G
24

Use var_export if you want a representation which is also valid PHP code

$a = array (1, 2, array ("a", "b", "c"));
$dump=var_export($a, true);
echo $dump;

will display

array (
 0 => 1,
 1 => 2,
 2 => 
 array (
   0 => 'a',
   1 => 'b',
   2 => 'c',
 ),
)

To turn that back into an array, you can use eval, e.g.

eval("\$foo=$dump;");
var_dump($foo);

Not sure why you would want to do this though. If you want to store a PHP data structure somewhere and then recreate it later, check out serialize() and unserialize() which are more suited to this task.

Gantline answered 15/9, 2009 at 8:43 Comment(4)
This is WRONG. It doesn't produce an array, it just makes $foo a string, which is equal to $dump, which is also a string, which should be parsed.Dominiquedominium
No it does not work. When I take the outputtet string, comment out your first two lines and set $dump to this outputted string (wither html or text), I get an syntax error. So it's different to use a var_export-ed variable and to only have a already var_dump-ed string.Bes
If you get a syntax error you're doing something wrong. I'd suggest you ask a new question.Gantline
I was having the same problem, and rather than having to tweak some code to turn my var_dump into the correct PHP multi-dimensional array syntax, I simply converted my array to YAML. This format is more readable and can fit in a single variable or in a hard .yml file. Then you can convert it as you like to php array or JSON. For me it is the easiest solution to store dynamic tables while keeping readability.Just
C
2

Maybe you’re looking for var_export that will give you a valid PHP expression of the passed value.

Centimeter answered 15/9, 2009 at 8:43 Comment(0)
M
2

I had a similar problem : a long runing script produced at the end a vardump of large array. I had to parse it back somehow for further analyzis. My solution was like this:

cat log.stats  | 
  sed 's/\[//g' | 
  sed 's/\]//g' | 
  sed -r 's/int\(([0-9]+)\)/\1,/g' | 
  sed 's/\}/\),/g' | 
  sed -r 's/array\([0-9]+\) \{/array(/g' > 
  log.stats.php
Macrae answered 6/2, 2012 at 11:43 Comment(2)
good answer, the only issue is if you have values contain strings ... so they get not wrapped by ''Anthropocentric
Definitely it was just a quick hack which worked for my specific scenario. In general var_dump is horrible to parse back, so whenever possible I use something else (mostly json_encode). You are right that strings would cause problems. Huge problems if contain slashes or quotes. Moderate if contain something which itself looks like var dump output (=>, [8] or int(12)) :)Macrae
A
1

You can't. var_dump just outputs text but doesn't return anything.

Alliterate answered 15/9, 2009 at 8:41 Comment(2)
What about one having a string from var_dump via goold old copy paste? (I know people who copy paste huge var_dumps to text-files and send those text-files to me.)Bes
@Seika85: You can tell them to use output buffering to get the result as a string. But for the purposes of this question this is still a »No« as they asked about getting an array.Alliterate
N
1

Perhaps you are trying to convert an object to an array? http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html

Nicosia answered 15/9, 2009 at 8:44 Comment(0)
S
1

var_export creates the PHP code, which you can run through the eval.

But I wonder, what is your idea?

Schlep answered 15/9, 2009 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.