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

来源:互联网 发布:战舰世界 爱宕数据 编辑:程序博客网 时间:2024/05/29 23:45

例,

$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 代码,且必须以分号结尾。


例子

<?php$string = "beautiful";$time = "winter";$str = 'This is a $string $time morning!';echo $str. "<br />";eval("\$str = \"$str\";");echo $str;?> 

输出:

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

0 0
原创粉丝点击