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?

ahundiak
—
2012-05-02T21:23:55Z —
#2

What version of php are you using? The works in any more or less recent version:

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

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

`

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 stringthen I do

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

then if I echo $data['params']['timezone'] I get this {

ScallioXTX
—
2012-05-02T22:34:33Z —
#4

json_decode doesn't work by reference, but returns the result. Try this

Brownie points for anyone who can explain where the { tlacaelelrl just got is coming from

tlacaelelrl
—
2012-05-03T00:58:41Z —
#5

ScallioXTX said:

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?

ralphm
—
2012-05-03T01:24:16Z —
#6

ScallioXTX said:

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?

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

tlacaelelrl
—
2012-05-03T06:29:07Z —
#7

ralph_m said:

Just a wild (php noob) shot in the dark here: does he need to escape those double quotes?

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...

ScallioXTX
—
2012-05-03T08:55:36Z —
#8

tlacaelelrl said:

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

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?

tlacaelelrl
—
2012-05-03T09:26:07Z —
#9

ScallioXTX said:

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

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!!