Json use in PHP

Hello all!

I have a string in this form


$somearray['params'] = "{"week":"42","timezone":"PDT"}";

and I am trying to use it like this


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?

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


error_reporting(E_ALL);

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

// 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'] . "\
";

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 {

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


<?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 :wink:

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?

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

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

I did not have to escape them, I just did what ScallioXTX said


$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…

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


$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? :slight_smile:

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!