5

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

Matt
  • 74,352
  • 26
  • 153
  • 180
omg
  • 136,412
  • 142
  • 288
  • 348
  • Does this answer your question? [Convert var\_dump of array back to array variable](https://stackoverflow.com/questions/3531857/convert-var-dump-of-array-back-to-array-variable) – ggorlen Jun 01 '22 at 01:15

6 Answers6

23

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.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • 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. – Megan Caithlyn Jul 08 '15 at 17:24
  • 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. – Seika85 Aug 25 '15 at 13:38
  • If you get a syntax error you're doing something wrong. I'd suggest you ask a new question. – Paul Dixon Aug 25 '15 at 13:52
  • 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. – Hugo-dev Aug 04 '21 at 16:33
2

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

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
Gumbo
  • 643,351
  • 109
  • 780
  • 844
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
qbolec
  • 5,374
  • 2
  • 35
  • 44
  • good answer, the only issue is if you have values contain strings ... so they get not wrapped by '' – WonderLand Jun 10 '14 at 20:36
  • 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)) :) – qbolec Aug 24 '14 at 21:50
1

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

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 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 Aug 25 '15 at 13:39
  • @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. – Joey Aug 26 '15 at 10:23
1

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

Palantir
  • 23,820
  • 10
  • 76
  • 86
1

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

But I wonder, what is your idea?

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
knittl
  • 246,190
  • 53
  • 318
  • 364