2016-10-09 3 views
0

У меня есть строка json. Но я не могу получить доступ к значениям.Php json decode array

$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']; 

Как я могу исправить этот код? Спасибо

+1

'file_get_contents ($ json_string)'? Что это? –

+0

Почему вы пытаетесь получить доступ к файлу, если у вас есть строка? – Rizier123

ответ

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']; 
1

Там нет необходимости использовать file_get_contents, если вы храните в JSON в виде строки, а затем декодирования. Выполните следующий подход:

$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 
+0

спасибо, помогло мне решить мою проблему. – sputn1k