0

i have a json string. But i can not access values.

$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$content = file_get_contents($json_string);
$json_a = json_decode($content, true);

echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];

How can i fix this code? Thank you

2
  • 1
    file_get_contents($json_string)? What is it? Commented Oct 9, 2016 at 13:09
  • Why do you try to access a file if you just have a string? Commented Oct 9, 2016 at 13:17

2 Answers 2

1
$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';

// you don't need this line
//$content = file_get_contents($json_string);
$json_a = json_decode($json_string, true);

echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];
Sign up to request clarification or add additional context in comments.

Comments

1

There's no need to use file_get_contents if you're storing the JSON in a string and then decoding it. Follow the below approach:

$json_string = '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$json_a = json_decode($json_string, true);

echo $json_a['05526']['0']['name']; // rapertuar
echo $json_a['05526']['0']['notes']['0']['mat1']; // 59

Comments

Your Answer

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