将关联数组编码为有效 php

Encode associative array as valid php

有没有办法将变量编码为可以作为 php 代码求值的字符串?特别是,我对关联数组很感兴趣。 (标量值和索引数组被 json_encode 编码为有效 php 代码。我不需要对对象和资源进行编码。

$Array = ['1', 2, 'key' => 'value'];
php_encode($Array); // => "[0 => '1', 1 => 2, 'key' => 'value']" or similar

您可以使用 var_export 并将第二个参数设置为 true:

function php_encode($val)
{
    return var_export($val, true);
}

http://php.net/manual/en/function.var-export.php