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']
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, )
Not sure if this is any use to you but found from http://php.net/manual/en/function.var-dump.php#77234
<?php
echo '<pre>'; // This is for correct handling of newlines
ob_start();
var_dump($var);
$a=ob_get_contents();
ob_end_clean();
echo htmlspecialchars($a,ENT_QUOTES); // Escape every HTML special chars (especially > and < )
echo '</pre>';
?>
var_export — Outputs or returns a parsable string representation of a variable. You also refer http://php.net/manual/en/function.var-export.php
<?php
var_export(array (1, 2, array ("a", "b", "c")));
?>
This will output like following:
array ( 0 => 1, 1 => 2, 2 => array ( 0 => 'a', 1 => 'b', 2 => 'c', ), )