php中怎么取出数组中的值,php怎样取出数组中的单个值

这次给大家带来php怎样取出数组中的单个值,php取出数组中的单个值的注意事项有哪些,下面就是实战案例,一起来看一下。

1.数组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);得到的是纯json

2. json_decode($object_json) 和 json_decode($json)得到的是数组对象

json_decode($object_json,true) 和 json_decode($json,true)得到的是数组

综上 , 可以将数组对象转为数组的方式:arr=jsondecode(jsonencode(arr=jsondecode(jsonencode(arr,true),true);

项目中发现此问题 , 建议大家在php中将json和array转换时 , json_encode() 和 json_decode()的第二个参数要加 true , 即:json_encode(arr,true);jsondecode(arr,true);jsondecode(json,true);

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值