SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Json use in PHP

  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Json use in PHP

    Hello all!

    I have a string in this form

    PHP Code:
    $somearray['params'] = "{"week":"42","timezone":"PDT"}"
    and I am trying to use it like this

    PHP Code:
    echo $somearray['params']['week']; 
    But that is not working, I have also tried json decode but that trows me an exception, right now I am doing explode but there must be a better way to do this, can someone point me in the right direction?
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    What version of php are you using? The works in any more or less recent version:
    PHP Code:
    error_reporting(E_ALL);

    // Just to see what json looks like
    $data = array('week' => 42'timezone' => 'PDT');
    echo 
    json_encode($data) . "\n";

    // Start with a string
    $json '{"week":42,"timezone":"PDT"}';

    // need the true to get an array
    $somearray = array();
    $somearray['params'] = json_decode($json,true);

    echo 
    $somearray['params']['week'] . "\n"

  3. #3
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    What version of php are you using? The works in any more or less recent version:
    PHP Code:
    error_reporting(E_ALL);

    // Just to see what json looks like
    $data = array('week' => 42'timezone' => 'PDT');
    echo 
    json_encode($data) . "\n";

    // Start with a string
    $json '{"week":42,"timezone":"PDT"}';

    // need the true to get an array
    $somearray = array();
    $somearray['params'] = json_decode($json,true);

    echo 
    $somearray['params']['week'] . "\n"
    For some reason that does not work for me, in the database I have a string exactly like this {"week":"42","timezone":"PDT"} and PHP reads it like this "{"week":"42","timezone":"PDT"}"
    I have an array of values from a row stored in $data, $data['params'] is that string
    then I do

    json_decode ( $data['params'], true )

    then if I echo $data['params']['timezone'] I get this {
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,080
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    json_decode doesn't work by reference, but returns the result. Try this

    PHP Code:
    <?php
    $data
    ['params'] = json_decode ($data['params'], true);
    echo 
    $data['params']['timezone'];
    Brownie points for anyone who can explain where the { tlacaelelrl just got is coming from
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Brownie points for anyone who can explain where the { tlacaelelrl just got is coming from
    LOL

    Yes that worked thank you!

    And actually I have been wondering where the { comes from since I first got it, I really can't understand why echoing $data['params']['timezone'] would give me that as response, if I got undefined index it would make sense but { why?
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  6. #6
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,200
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Brownie points for anyone who can explain where the { tlacaelelrl just got is coming from
    Just a wild (php noob) shot in the dark here: does he need to escape those double quotes?

    PHP Code:
    "{\"week\":\"42\",\"timezone\":\"PDT\"}" 

  7. #7
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    Just a wild (php noob) shot in the dark here: does he need to escape those double quotes?

    PHP Code:
    "{\"week\":\"42\",\"timezone\":\"PDT\"}" 
    I did not have to escape them, I just did what ScallioXTX said

    PHP Code:
    $data['params'] = json_decode ($data['params'], true); 
    I had done that and it did not work because I was not adding the second parameter which in the PHP manual says it is to make an associative array which is what I was trying to do!

    About getting { when doing echo $data['params']['timezone'] I have no idea how that worked so...
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,080
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by tlacaelelrl View Post
    LOL

    Yes that worked thank you!

    And actually I have been wondering where the { comes from since I first got it, I really can't understand why echoing $data['params']['timezone'] would give me that as response, if I got undefined index it would make sense but { why?
    You don't get the 'undefined index' error because at that point $data['params'] is a string, not an array and strings don't have (it would seem) invalid indices. What you can do with strings is get the character on a given position using the same notation as array indices, so for example

    Code:
    $str = 'Hello';
    echo $str[0]; // H
    echo $str[1]; // e
    echo $str[2]; // l
    echo $str[3]; // l
    echo $str[4]; // o
    So what was happening with you is that PHP found a string and saw that you wanted to offset (take the character at a certain position using []) it like above, but it doesn't understand the index 'week', so what it does is it casts 'week' to an integer, so you get 0, and then it takes the character at position 0 of your json string, which happens to be the {

    Does that make sense?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    You don't get the 'undefined index' error because at that point $data['params'] is a string, not an array and strings don't have (it would seem) invalid indices. What you can do with strings is get the character on a given position using the same notation as array indices, so for example

    Code:
    $str = 'Hello';
    echo $str[0]; // H
    echo $str[1]; // e
    echo $str[2]; // l
    echo $str[3]; // l
    echo $str[4]; // o
    So what was happening with you is that PHP found a string and saw that you wanted to offset (take the character at a certain position using []) it like above, but it doesn't understand the index 'week', so what it does is it casts 'week' to an integer, so you get 0, and then it takes the character at position 0 of your json string, which happens to be the {

    Does that make sense?
    Yes, it actually makes sense now that you explain it that way, it was so weird for me to see that bracket printed!!

    Thank you for all your help!
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •