无法在 php 中打印 json 数组值

您好,以下是我打印的输出,

{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}

我需要将每个值确认、消息、状态存储在单独的 php 变量中,我该怎么做。以下是我的代码

var_dump(json_decode($server_output));
echo $server_output["Confirmation"];


沧海一幻觉
浏览 98回答 2
2回答

慕容森

您可以通过多种不同的方式来做到这一点,看看不同的方法及其演示。使用extract(),<?php$str = '{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}';extract(json_decode($str,true));echo $Confirmation;?>演示: https:&nbsp;//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;?>演示::&nbsp;https :&nbsp;//3v4l.org/jLnRE使用(从php 7.1array destructure开始),<?php$str = '{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}';['Confirmation'=>$confirmation, 'Message'=>$message, 'Status'=>$status] =&nbsp; json_decode($str,true);echo $confirmation;?>演示: https:&nbsp;//3v4l.org/rrFa8使用list(),<?php$str = '{"Confirmation":"200710035843DH4","Message":"success","Status":"success"}';list('Confirmation'=>$confirmation, 'Message'=>$message, 'Status'=>$status) =&nbsp; json_decode($str,true);echo $confirmation;?>演示: https:&nbsp;//3v4l.org/Zpt60

UYOU

您可以使用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;工作演示
打开App,查看更多内容
随时随地看视频慕课网APP