PHP如何将这样的字符串变成数组 本帖最后由 tangl198710 于 2011-08-12 12:11:55 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $str = <<<EOF{'aid':'21','ctl_a_cpu':'df','ctl_a_ram':'df','ctl_a_disk':'df','ctl_a_fip':'df','ctl_a_os':'c_1_2','ctl_a_os_lang':'c_4_1,c_4_3','comment':'其它要求\',\'呵呵\',\'','total':'1090','typeid':'6'}EOF;$ar = json_decode($str, true);print_r($ar); json_decode 使用这个函数有什么特别注意的东西没? 我打印出来是空白,已找到其它方法解决了。但想知道 json_decode 这个函数怎么使用的 楼主,你那个格式不正确不知道你用什么工具输出的json数据啊楼主来看看这个例子$str1 ='{"a":21}';$str2="{\"b\":21}";$str3="{'c':21}";print_r(json_decode($str1,true));print_r(json_decode($str2,true));print_r(json_decode($str3,true)); 输出结果为Array ( [a] => 21 ) Array ( [b] => 21 ) 第三个print_r输出为空白 json_decode ( string $json [, bool $assoc ] )接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 参数json 待解码的 json string 格式的字符串。 assoc 当该参数为 TRUE 时,将返回 array 而非 object 。 同上,直接json_decode这个字符串。这样写:json_decode($str, true);注意如果不加“true”的话,转化成的是对象,加了true以后才是数组 恩。。果然无法直接用json_decode()处理,改手工方式转换:$str = <<<EOF{'aid':'21','ctl_a_cpu':'df','ctl_a_ram':'df','ctl_a_disk':'df','ctl_a_fip':'df','ctl_a_os':'c_1_2','ctl_a_os_lang':'c_4_1,c_4_3','comment':'其它要求\',\'呵呵\',\'','total':'1090','typeid':'6'}EOF;$ar = explode("','", substr($str, 2, -2));$result = '';foreach($ar as $v) { $ar_tmp = explode("':'", $v); $result[$ar_tmp[0]] = $ar_tmp[1];}echo '<pre>';print_r($result); PHP数组基础http://3aj.cn/php/39.html 页面文字显示正常,对话框乱码!何解? php 继承包含父类的问题 关于php 关于apache优化方面的问题请教 能不能推荐一款简单传统的PHP搜索引擎源代码给我? 100分问$_POST值成双倍了!!!,中间还有input控件名称 关于动态变量如何用 急!一个selected动态定位的问题 请教一下,www.go2map.com 的寻找 最佳路线的算法如何实现的,给分 php对话 php操作文件 数组切割 在线等
{'aid':'21','ctl_a_cpu':'df','ctl_a_ram':'df','ctl_a_disk':'df','ctl_a_fip':'df','ctl_a_os':'c_1_2','ctl_a_os_lang':'c_4_1,c_4_3','comment':'其它要求\',\'呵呵\',\'','total':'1090','typeid':'6'}
EOF;
$ar = json_decode($str, true);
print_r($ar);
不知道你用什么工具输出的json数据啊楼主来看看这个例子$str1 ='{"a":21}';
$str2="{\"b\":21}";
$str3="{'c':21}";
print_r(json_decode($str1,true));
print_r(json_decode($str2,true));
print_r(json_decode($str3,true));
输出结果为Array ( [a] => 21 ) Array ( [b] => 21 ) 第三个print_r输出为空白
接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 参数
json
待解码的 json string 格式的字符串。 assoc
当该参数为 TRUE 时,将返回 array 而非 object 。
这样写:
json_decode($str, true);
注意如果不加“true”的话,转化成的是对象,加了true以后才是数组
$str = <<<EOF
{'aid':'21','ctl_a_cpu':'df','ctl_a_ram':'df','ctl_a_disk':'df','ctl_a_fip':'df','ctl_a_os':'c_1_2','ctl_a_os_lang':'c_4_1,c_4_3','comment':'其它要求\',\'呵呵\',\'','total':'1090','typeid':'6'}
EOF;$ar = explode("','", substr($str, 2, -2));$result = '';
foreach($ar as $v) {
$ar_tmp = explode("':'", $v);
$result[$ar_tmp[0]] = $ar_tmp[1];
}
echo '<pre>';
print_r($result);
PHP数组基础
http://3aj.cn/php/39.html