yamcsha

json_decode example

Mar 4th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.43 KB | None | 0 0
  1. <?php
  2.  
  3. $json = '[{
  4.    "user_id": "1",
  5.    "username": "Sandy Corzeta"
  6. }]';
  7.  
  8. // array of users objects as is in the json code
  9. $users = json_decode($json);
  10.  
  11. if (null === $users && json_last_error() !== JSON_ERROR_NONE) {
  12.     die('Error decoding json, error code: ' . json_last_error());
  13. }
  14.  
  15. ?>
  16. <div>
  17.  <div id="user_id"><?php echo $users[0]->user_id; ?></div>
  18.  <div id="username"><?php echo $users[0]->username; ?></div>
  19. </div>
Advertisement
Add Comment
Please, Sign In to add comment