如何创建描述人物的对象。该对象应具有以下属性:
输出应采用以下格式:
答案 0 :(得分:0)
var object = {
firstName: "Joe",
lastName: "Smith",
address: "123 Main Street",
phones: [
{
type: "mobile",
number: "123-123-1234"
},
{
type: "home",
number: "321-321-4321"
}]
};
var json_string = JSON.stringify(object);
如果JSON字符串在PHP的$json
中,您可以通过以下方式获取手机号码:
$array = json_decode($json, true);
foreach ($array['phones'] as $phone) {
if ($phone['type'] == 'mobile') {
echo $phone['number'];
break;
}
}