How to create an array from var_dump output in PHP?

How can I parse the output var_dumpin PHP to create an array?

+5
source share
6 answers

Use var_export if you want a view that 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 return this back to an array, you can use eval, for example.

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

Not sure why you would like to do this. If you want to save the PHP data structure somewhere, and then recreate it later, check serialize () and unserialize () , which are more suitable for this task.

+24
source

. var_dump , .

+1

, var_export, PHP .

+1

var_export PHP, eval.

, ?

+1

: script, vardump . - . :

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
+1

All Articles