-1

i got this json encoded data being sent to me. can u tell me how to get each of the individual elements ? Something like:

$ticket
$customer
$user


{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me@site.com","emails":["me@site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}

this is a basic view on how my code runs.

            $ret = array('html' => '');
            $data = json_decode($data , true);

            $ret['html'] = '<ul><li>'.$data->ticket->number.'</li></ul>';

            echo json_encode($ret);
            exit;

it only prints the circle from the li tags.

misulicus
  • 437
  • 2
  • 6
  • 16
  • http://php.net/manual/en/function.json-decode.php You should try looking up a way to do something before posting on here. – Lugubrious Sep 06 '13 at 23:40

2 Answers2

1

json_decode is the answer for you.

Cthulhu
  • 1,379
  • 1
  • 13
  • 25
1

To clarify @Cthulhu's answer :

$test = '{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me@site.com","emails":["me@site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}';
$data = json_decode($test);
echo $data->ticket->id;

outputs

10909446

json_decode make the JSON into a stdClass object, and then you can access the values as that.


$data = json_decode($test);
$ret = array();
$ret['html']='<ul><li>'.$data->ticket->number.'</li></ul>';

return

json_encode($ret);

will return

{"html":"<ul><li>152<\/li><\/ul>"}
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • i tried it mate but it seems its not working. That code is sent from another webpage to my php file. i have to extract those values to perform other code updates and the json_encode what i want to send back to the initial webpage i tried something like $data = json_decode($data); //do stuff echo json_encode($data->ticket->id); just to see if the ticket id is sent back but its not – misulicus Sep 06 '13 at 23:53
  • Your JSON may be invalid, so. The input JSON you have given here is OK. json_encode is to if you have created your result as an array - have you done that? – davidkonrad Sep 06 '13 at 23:58
  • remember `json_encode($ret, true)` if you have deep nested array values – davidkonrad Sep 07 '13 at 00:38