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 關注
- 129 瀏覽
添加回答
舉報