2 回答
TA贡献1853条经验 获得超18个赞
您可以通过多种不同的方式来做到这一点,看看不同的方法及其演示。
使用extract(),
<?php
$str = '{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}';
extract(json_decode($str,true));
echo $Confirmation;
?>
演示: https: //3v4l.org/3U43b
使用foreach(),
<?php
$str = '{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}';
$array = json_decode($str,true);
foreach ( $array as $key => $value ) { $$key = $value; }
echo $Confirmation;
?>
演示:: https : //3v4l.org/jLnRE
使用(从php 7.1array destructure开始),
<?php
$str = '{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}';
['Confirmation'=>$confirmation, 'Message'=>$message, 'Status'=>$status] = json_decode($str,true);
echo $confirmation;
?>
演示: https: //3v4l.org/rrFa8
使用list(),
<?php
$str = '{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}';
list('Confirmation'=>$confirmation, 'Message'=>$message, 'Status'=>$status) = json_decode($str,true);
echo $confirmation;
?>
演示: https: //3v4l.org/Zpt60
TA贡献1878条经验 获得超4个赞
您可以使用extract(),但这会将变量从数组导入到当前符号表中。如果您想控制变量名称,可以使用list():
<?php
$json = '{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}';
$arr = json_decode($json, true);
list('Confirmation' => $conf, 'Message' => $msg, 'Status' => $status) = $arr;
echo $conf;
echo $msg;
echo $status;
工作演示
- 2 回答
- 0 关注
- 97 浏览
添加回答
举报