如何从php中的json数组中获取数据

时间:2016-02-25 05:17:50

标签: javascript php arrays json ajax

我试图从PHP变量中获取此后续JSON数组的值。

这是数组的var_dump:

array(1) { [0]=> object(stdClass)#1 (1) { ["row"]=> object(stdClass)#2 (1) { ["u_fname"]=> string(6) "Ashish" } } }

这是我的页面代码 $字符只是window.jQuery的快捷方式。     

$contents = file_get_contents('http://localhost/skillbook/json.php');
$contents = utf8_encode($contents);
$results =var_dump(json_decode($contents)); 
?>

2 个答案:

答案 0 :(得分:1)

$contents = file_get_contents('http://localhost/skillbook/json.php');
$contents = utf8_encode($contents);
$contents_array = json_decode($contents,1);

$foundvalue= $contents_array[0]["row"]["u_fname"];
echo $foundvalue;

OutPut: Ashish

答案 1 :(得分:0)

我认为你可以为json使用foreach条件

$json = file_get_contents('http://localhost/skillbook/json.php'); 
$json = utf8_encode($json);
$data = json_decode($json,true);

// change the variable name to devices which is clearer.
$devices = $data['0'];
foreach ($devices as $device)
{
   echo $device["u_fname"];
}