2 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
您可以通過(guò)多種不同的方式來(lái)做到這一點(diǎn),看看不同的方法及其演示。
使用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開(kāi)始),
<?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貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以使用extract(),但這會(huì)將變量從數(shù)組導(dǎo)入到當(dāng)前符號(hào)表中。如果您想控制變量名稱,可以使用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 關(guān)注
- 137 瀏覽
添加回答
舉報(bào)