Is there a way how to "encode" PHP array into PHP code?

Frodik :

for very special purpose I need to transform PHP array into PHP code. I will try to explain what I mean in code below:

$a = array('a' => 'abc', 'path' => INCLUDE_DIR.'/file.txt', 'number' => 1234);
$php_code = some_magic_function($a);
echo $php_code; // outputs: "array('a' => 'abc', 'path' => '/path/to/file.txt', 'number' => 1234)"

So it would write back array as PHP code. I can not find any library that would help me with this.

Any ideas or hints ? Thanks in advance.

ADyson :

I think you want to print the array as a string, in the exact format it would be written in your code.

If so, then the var_export() function is what you need, e.g. something like this:

$a = array('a' => 'abc', 'path' => '/file.txt', 'number' => 1234);
$str = var_export($a, true);
echo $str;

Demo: http://sandbox.onlinephpfunctions.com/code/0b1da8fc0199a34539a55313680e982f9fddd14f

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10497&siteId=1