Anna Jeanine Anna Jeanine - 1 month ago 8
JSON Question

Get data for JSON file PHP

This may seem like a duplicate question, but my JSON is formatted differently than other examples I have seen. The which I am using:

$str = file_get_contents($tmp);
$json = json_decode($str, true);
$project_id = $json["project_id"];


My JSON is formatted like:

[{"project_id": 2.0, "name": "Anna", "place": "Amsterdam", "date": "31 October 2016"}]


The error which I am getting:

Undefined index: project_id


Could someone help me out?

Answer

Try this (note the [0]):

$project_id = $json[0]["project_id"];