【发布时间】:2016-03-13 04:42:06
【问题描述】:
我想我错过了明显的地方,解析格式参数的最佳方法是什么......
'{"phonenumber":"123456", "mobile":"589521215", "website":"www.xfty.co.uk" }'
这样我就有了各个变量?
【问题讨论】:
我想我错过了明显的地方,解析格式参数的最佳方法是什么......
'{"phonenumber":"123456", "mobile":"589521215", "website":"www.xfty.co.uk" }'
这样我就有了各个变量?
【问题讨论】:
如果你有一个带有 JSON 内容的字符串:
$string = '{"phonenumber":"123456","mobile":"589521215","website":"www.xfty.co.uk"}';
你可以解析它并得到一个带有json_decode的关联数组:
$array = json_decode( $string, true );
并通过以下方式访问数组:
$array["phonenumber"] //will give back '123456'
或者,如果您希望获得 stdClass 对象,只需使用:
$object = json_decode( $string );
然后得到它:
$object->phonenumber; //will give back '123456'
【讨论】:
您的输入看起来像 json 格式。您应该使用 json_decode() 来访问它的值:
<?php
$values = json_decode('{"phonenumber":"123456","mobile":"589521215","website":"www.xfty.co.uk"}');
echo "Phonenumber:" . $values->phonenumber . "<br/>";
echo "Mobile:" . $values->mobile . "<br/>";
echo "Website:" . $values->website . "<br/>";
【讨论】: