Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - retrieve first element of json string without converting to array

Tags:

json

php

I have a json string, which I am currently probing as follows:

$playedOn = $query -> data -> artist -> services_played_on

I now want to get the first element of this services_played_on, but am unsure how to do this.. If it were an array I'd just do $query[data][artist][services_played_on][0], but for various reasons this would mess up other code that I have. Anyone have any ideas how I can do this using the -> notation?

-----EDIT-----

Thanks for your answers, but what I needed was:

$playedOn = $query -> data -> artist -> services_played_on[0]

(I can't submit this as an answer yet, as I am a 'new' user)

like image 632
tiswas Avatar asked May 11 '11 12:05

tiswas


1 Answers

Just decode the JSON first.

$jset = json_decode($json, true);
echo $jset["data"]["artist"]["services_played_on"][0];
like image 66
Marcel Avatar answered Nov 14 '22 21:11

Marcel