0

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?

1
  • 1
    $project_id = $json[0]["project_id"]; Commented Oct 31, 2016 at 12:49

2 Answers 2

1

Try this (note the [0]):

$project_id = $json[0]["project_id"];
Sign up to request clarification or add additional context in comments.

Comments

0

Missing one level

$str = file_get_contents($tmp);
$json = json_decode($str, true);
foreach($json as $j){
 echo $j['project_id'];
}

Your response datas is an Array of jsons object so have to loop on it.

Also, if you want bypass loop and get directly first result you can do this :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.