-3

In fact Im having some troubles with php script that i have made recently. The problem is that I can't get data from a json file damo.json using php. This is the code of the json file:

{ "checkouts":[
    {
    "billing_address":{
    "country":"Italy",
    "first_name":"christian"
    }
    },
    {
    "billing_address":{
    "country":"Italy",
    "first_name":"christian"
    }
    }
    ]
    }

i want to get the first_name record. Is it possible using php ? this is the php code:

<?php
$data = file_get_contents('demo.json');
$obj = json_decode($data);
foreach ($obj->billing_address as $result)
{
    echo $result->name;
}

?>  

1 Answers1

0

After you fix your syntax as noted above load the file and use json_decode with first parameter the json and second parameter true for associative array.

$data = file_get_contents( "demo.json" );
$data = json_decode($data, true);
var_dump($data);//you have assoc array
v.radev
  • 136
  • 1
  • 7
  • 1
    Well, correct me if I am lost, but, the people does not answer this question because the user did not take the time to read and investigate a relative easy question that could be resolved with a research at this site. – manix Oct 13 '14 at 18:05