0

I'm trying to get the value from this following JSON array in a PHP variable.

This is a var_dump of the array:

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

This is my code of the page

<?php 

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

However when I tried to get a service from online the file_get_contents function is not working.

file_get_contents(http://...@gmail.com&pass=12345): failed to open stream: HTTP request failed! HTTP/1.1 403 ModSecurity Action in C:\xampp\htdocs\skillbook\json1.php

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

2 Answers2

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

Deepak Mankotia
  • 4,404
  • 6
  • 30
  • 55
  • He wanted the value in a PHP variable, so you'd want the last line to be: $foundvalue = $contents_array[0]["row"]["u_fname"]; – David Feb 25 '16 at 05:29
  • Dear Deepak when i tried to get a service from online the file_get_contents function not working ...the error comes like this file_get_contents(http://...@gmail.com&pass=12345): failed to open stream: HTTP request failed! HTTP/1.1 403 ModSecurity Action in C:\xampp\htdocs\skillbook\json1.php on line 9 – Ashish Biswal Feb 25 '16 at 05:43
  • Thanks Deepak Mankotia and David – Ashish Biswal Feb 25 '16 at 05:47
0

i think you can use foreach condition for json

$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"];
}
Dhaval Patel
  • 1,076
  • 6
  • 19