Php 从json检索键和值
我有一个JSON字符串:Php 从json检索键和值,php,json,Php,Json,我有一个JSON字符串: $json= '{"data":[{"id":"123","name":"john smith","gender":"MALE","phone":[{"number":"+919999999999","numberType":"MOBILE"}]}]}' 我想从json中检索所有值和键,输出应该如下所示: id: 123 name: john smith gender: MALE phone: number: +91
$json= '{"data":[{"id":"123","name":"john smith","gender":"MALE","phone":[{"number":"+919999999999","numberType":"MOBILE"}]}]}'
我想从json中检索所有值和键,输出应该如下所示:
id: 123
name: john smith
gender: MALE
phone:
number: +919999999999
numberType: MOBILE
我已尝试此代码,但无法获得手机输出:
$jsond = json_decode($json);
foreach($jsond->data as $row)
{
foreach($row as $key => $val)
{
echo $key . ': ' . $val;
}
}
这正是我们的目的:
是否允许我们重构您的json。或者,请在您的数据中演示多个人。Hey@Jonathan根据请求,输出将仅为一个人,是的,我们被允许重构相关的:
<?php
$json = '{"data":[{"id":"123","name":"john smith","gender":"MALE","phone":[{"number":"+919999999999","numberType":"MOBILE"}]}]}';
$jsond = json_decode($json,true);
function test_print($val, $key)
{
echo "$key : $val<br/>\n";
}
array_walk_recursive($jsond, 'test_print');