-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;
}

?>  
6
  • 1
    That JSON is not valid JSON. So - probably, once you munge your custom data format into something recognisable. Commented Oct 13, 2014 at 17:57
  • @Quentin ive just made mistake while copying the code Commented Oct 13, 2014 at 17:59
  • You missed the first opening curl brace at the beginning. Commented Oct 13, 2014 at 17:59
  • 1
    @YassineDevlopper — Then the answer is still "yes" and you should read PHP's JSON documentation. php.net/json Commented Oct 13, 2014 at 18:00
  • 2
    why do not you put also the php script that you made? Commented Oct 13, 2014 at 18:03

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.