我正在尝试从PHP变量中的以下JSON数组中获取值。
这是数组的一个var_dump:
array(1) { [0]=> object(stdClass)#1 (1) { ["row"]=> object(stdClass)#2 (1) { ["u_fname"]=> string(6) "Ashish" } } }
这是我的页面代码
<?php
$contents = file_get_contents('http://localhost/skillbook/json.php');
$contents = utf8_encode($contents);
$results =var_dump(json_decode($contents));
?>
但是,当我尝试从在线获取服务时,file_get_contents
功能不起作用。
file_get_contents(http://...@gmail.com&pass=12345):无法打开流:HTTP 请求失败!HTTP/1.1 403 ModSecurity Action in C:''xampp''htdocs''skillbook''json1.php
$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;
输出 : 阿希什
我认为你可以对 JSON 使用 for each 条件
$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"];
}