将关联数组编码为有效的php

时间:2016-05-20 09:13:28

标签: php encode

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

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

1 个答案:

答案 0 :(得分:5)

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

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

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

相关问题