0

I have the fallowing string in php:

    $code = "[[],["Mon","01","  1.7","  8","3"," 96","33","
29.01.2013"],["Tue","01","  0.3"," 24","2","100","16","
30.01.2013"],["Wed","01","  5.4"," 28","2"," 98","5","
31.01.2013"],["Thu","01","  8.7"," 22","3"," 92","23","
01.02.2013"],["Fri","01","  5.1"," 43","3"," 91","22","
02.02.2013"],["Sat","01","  2.8"," 18","2"," 90","22","
03.02.2013"],["Sun","01","  2.1"," 31","6"," 93","34","
04.02.2013"]]";

Now i try to decode this string with json_decode. But the result ist this one:

NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

The code to output is this:

$code = json_decode($code);
print_r($code);

Why this dont work ? THis is the first time i have problems with json_decode ...

3
  • 2
    You are saying $code = .... is all inside a string? That's not valid JSON then. Commented Jan 28, 2013 at 16:09
  • First, that's an array. Not a string. Second that's not JSON. Commented Jan 28, 2013 at 16:11
  • is `$code = "..." meant to be a PHP string? It's invalid PHP because of the quotes. The code you've quoted shouldn't even run, let alone produce all those NULLs. Commented Jan 28, 2013 at 16:37

3 Answers 3

3

Assuming that the contents of $code are all in a string (and not a php array like the syntax is now), the error is the fact that you have newlines inside of the strings.

["Mon","01","  1.7","  8","3"," 96","33","

Notice how there is an open quote at the end of the line .. that makes for invalid JSON.

If you get rid of all of the newlines, it actually does work. Here's my proof:

array(8) {
  [0]=>
  array(0) {
  }
  [1]=>
  array(8) {
    [0]=>
    string(3) "Mon"
    [1]=>
    string(2) "01"
    [2]=>
    string(5) "  1.7"
    [3]=>
    string(3) "  8"
Sign up to request clarification or add additional context in comments.

2 Comments

Ah! This cann solve the problem...thanks a lot... got the string by database so maybe the format was wrong i saved it. Thanks.
problem solved! $d->data = str_replace(array("\r\n", "\r", "\n"), '', $d->data); fixed it.
0

It doesn't work because it is not valid JSON. You can find the correct JSON formatting here: http://www.w3schools.com/json/default.asp

2 Comments

Not valid JSON ? But got thsi string by using json_encode on a php-array! How can this be ?
If you could edit you're original post with the full encoding of the array included, I may be able to see if there is something going wrong at the encoding end.
0

This does not seems to be like a valid json string.You would get the required result if you try it with a valid json string like

<?php
$code=... //a valid json string
$result=json_decode($code,true); // now $result will contain an associative array
print_r($result);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.