Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get variable definition as string

Tags:

arrays

php

Is there any built-in way to get an array's definition in string format? The out put should be valid PHP code for defining the same array.

For example:

$arrayDefinition = array_encode($anArray);

Should return something like:

['a' => 'x', 'b' => 'y']
like image 755
Handsome Nerd Avatar asked Dec 19 '22 04:12

Handsome Nerd


1 Answers

I think you are looking for var_export().

Example:

$arr = [1,2,3];
echo $str = var_export($arr, TRUE);

output:

array ( 0 => 1, 1 => 2, 2 => 3, )
like image 87
Rizier123 Avatar answered Jan 06 '23 06:01

Rizier123