php - 字符串内容为数组如何进行格式转换为数组

例,

$str    =    "array( 'USD'=>'1', 'GBP'=>'0.6494', 'EUR'=>'0.7668' ,'JPY'=>'82.8150','RMB'=>'6.6480' )";

现有$str,内容为数组形式的数据。

想要得到真正的数组

$arr    = array( 'USD'=>'1', 'GBP'=>'0.6494', 'EUR'=>'0.7668' ,'JPY'=>'82.8150','RMB'=>'6.6480' );

处理办法是:

eval("\$arr = ".$str.'; ');

这样就得到数组$arr为字符串$str的数据的数组形式

跟js使用eval类似。。


定义和用法

eval() 函数把字符串按照 PHP 代码来计算。

该字符串必须是合法的 PHP 代码,且必须以分号结尾。


例子

";

eval("\$str = \"$str\";");
echo $str;
?> 

输出:

This is a $string $time morning!
This is a beautiful winter morning! 

你可能感兴趣的:(php - 字符串内容为数组如何进行格式转换为数组)