Accessing field in decoded json string PHP -
i'm trying access field 'id' of first song in decoded json string using php. i've tried possible combinations one:
$response->songs[0]->id
this decoded json string:
array ( [response] => array ( [status] => array ( [version] => 4.2 [code] => 0 [message] => success ) [songs] => array ( [0] => array ( [artist_id] => arrh63y1187fb47783 [id] => sokgwes13d647be466 [artist_name] => kanye west [title] => of lights ) [1] => array ( [artist_id] => arrh63y1187fb47783 [id] => sohbkvu14509a9f6c3 [artist_name] => kanye west [title] => of lights ) [2] => array ( [artist_id] => arrh63y1187fb47783 [id] => sodelay13ad1acc8cf [artist_name] => kanye west [title] => of lights ) [3] => array ( [artist_id] => arrh63y1187fb47783 [id] => soudiym14b7c7b2d95 [artist_name] => kanye west [title] => of lights ) [4] => array ( [artist_id] => arrh63y1187fb47783 [id] => sotempj13db921f71f [artist_name] => kanye west [title] => of lights ( remix ) ) [5] => array ( [artist_id] => arrh63y1187fb47783 [id] => soxidrl13ccfbbc829 [artist_name] => kanye west [title] => of lights [lbluke rmx] ) [6] => array ( [artist_id] => arrh63y1187fb47783 [id] => sotjzso12d857905f6 [artist_name] => kanye west [title] => of lights ( interlude ) ) [7] => array ( [artist_id] => arvcdgf12fe08689ba [id] => soglujd130516e0d00 [artist_name] => made famous kanye west [title] => of lights ) ) ) )
thanks in advance
$id = $json_array['response']['songs'][0]['id'];
explanation
take @ response, response multidimensional array, means have array formed several arrays, , each 1 can contain 1 or more arrays.
in first array have "response", 1 contains rest of them, so..
$id = $json_array['response']
from array have go nesting inside until wanted element. contained array songs, so..
$id = $json_array['response']['songs']
this 1 has several elements indexed numbers, want id first song pick 0 element,
$id = $json_array['response']['songs'][0]
after can element want:
$id = $json_array['response']['songs'][0]['id'];
Comments
Post a Comment