数组arr
var_dump(arr) 值如下:
array (size=3) 'delete' =>
array (size=3) 0 => string 'HBSFlyRecode20170222-101501.txt' (length=31) 1 => string 'HBSFlyRecode20170222-105502.txt' (length=31) 2 => string 'HBSFlyRecode20170222-108803.txt' (length=31) 'new' =>
array (size=3) 0 => string 'HBSFlyRecode20170223-101504.txt' (length=31) 1 => string 'HBSFlyRecode20170223-105505.txt' (length=31) 2 => string 'HBSFlyRecode20170223-108806.txt' (length=31) 'old' =>
array (size=3) 0 => string 'HBSFlyRecode20170221-101507.txt' (length=31) 1 => string 'HBSFlyRecode20170221-105508.txt' (length=31) 2 => string 'HBSFlyRecode20170221-108809.txt' (length=31)echo $arr['old'][0]; 打印出: HBSFlyRecode20170221-101507.txt
但是如果arr是对象形式 , 打印结果如下:
var_dump(arr)
object(stdClass)[1] public 'delete' =>
array (size=3) 0 => string 'HBSFlyRecode20170222-101501.txt' (length=31) 1 => string 'HBSFlyRecode20170222-105502.txt' (length=31) 2 => string 'HBSFlyRecode20170222-108803.txt' (length=31) public 'new' =>
array (size=3) 0 => string 'HBSFlyRecode20170223-101504.txt' (length=31) 1 => string 'HBSFlyRecode20170223-105505.txt' (length=31) 2 => string 'HBSFlyRecode20170223-108806.txt' (length=31) public 'old' =>
array (size=3) 0 => string 'HBSFlyRecode20170221-101507.txt' (length=31) 1 => string 'HBSFlyRecode20170221-105508.txt' (length=31) 2 => string 'HBSFlyRecode20170221-108809.txt' (length=31)就不能使用 $arr[‘old’][0] 取值了 , 可以使用arr对象和数组通用的foreach方式取值:
function getValue($arr){
foreach($arr as $key => $value){ if(is_array($value)){
getValue($value);
}else{ echo $value."
";
}
}
}如果arr为对象形式 , 可以考录将对象转为数组形式 , 这里提供一种快捷方式:
1. $object_json = json_encode($arr);得到的是对象 $json = json_encode($arr,true);得到的是纯json2. json_decode($object_json) 和 json_decode($json)得到的是数组对象
json_decode($object_json,true) 和 json_decode($json,true)得到的是数组
综上 , 可以将数组对象转为数组的方式:
立即学习“PHP免费学习笔记(深入)”;
[PHP房产程序|BBWPS]功能介绍 1、5种信息类别发布:出租、求租、出售、求购、楼盘信息,支持会员发布信息审核; 2、灵活的信息参数设置; 3、充足的信息字段; 4、简单易用的发布/编辑功能,支持配图上传; 5、灵活的信息管理功能; 6、信息输出伪静态,方便搜索引擎抓取数据; 7、支持RSS输出; 8、内置数据高速缓冲技术,可灵活设置缓冲功能是否启动及过期时间; 9、支持 Google 地图
arr,true),true);
项目中发现此问题 , 建议大家在php中将json和array转换时 , json_encode() 和 json_decode()的第二个参数要加 true , 即:
json_encode(
json,true);
更多相关教程请访问 php编程从入门到精通全套视频教程










