6

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

1

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
If you get a syntax error you're doing something wrong. I'd suggest you ask a new question.
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.
2

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

Comments

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

2 Comments

good answer, the only issue is if you have values contain strings ... so they get not wrapped by ''
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)) :)
1

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

2 Comments

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.)
@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.
1

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

Comments

1

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

But I wonder, what is your idea?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.