2

On MySQL I have this JSON value

{"length":1847,"data":[0,0,0,0,-46,37]}

On PHP I need to string in array the "data" objects, so I tried:

$json = $row['wave'];
$json_array = json_decode($json);
$json_wave = $json_array["data"];

It gives me internal server error 500, so I tried:

var_dump(json_decode($json));

What I got is:

object(stdClass)#7 (5) {
  ["length"]=>
  int(1847)
  ["data"]=>
  array(3694) {
    [0]=>
    int(0)
    [1]=>
    int(0)
    [2]=>
    int(0)
    [3]=>
    int(0)
    [4]=>
    int(-46)
    [5]=>
    int(37)
  }
}

I need to output this string 0,0,0,0,-46,37, why is my code not working?

1
  • 2
    add second parameter TRUE into $json_array = json_decode($json,TRUE); to make it associative array then use $json_wave = $json_array["data"]; Commented Aug 9, 2016 at 13:39

6 Answers 6

4

If you want decode JSON to array you shoud use json_decode function with additional parameter ($assoc as true):

$json_array = json_decode($json, true);

You can look to documentation here: http://php.net/manual/en/function.json-decode.php

Sign up to request clarification or add additional context in comments.

Comments

2

json_decode() by default returns an Object instead of an array. If you want to decode the JSON String in Array json_decode accepts second parameter as boolean.

If the second parameter is true json_decode() will return the value in forms of array.

So your code will look something like this,

$json = $row['wave'];
$json_array = json_decode($json,true);
$json_wave = $json_array["data"];

Second parameter's by default value is false.

To get the data from Object you can access it like this,

print_r($json_array->data);

Comments

2

So assuming you want that last variable to be the string you need this

$json_array = json_decode($json);
$json_wave = implode(',', $json_array->data);

Because your JSON contains an object, PHP puts it into an object by default. That's what var_dump means when it says stdClass

Comments

0

Your var_dump shows that the row is decoded as an object, so you cannot access its fields using array notation.

Change

$json_wave = $json_array["data"];

To

$json_wave = $json_array->data;

Alternatively, if you want to work with arrays rather than objects, you can set the 2nd parameter to json_decode as below:

$json_array = json_decode($json, true);

Comments

0

It doesn't work because you're trying to treat object like an array. json_decode wihtout second parameter converts json_string to an object. use json_decode($json, true) to say 'hey, convert my json string to an array, not object'. On the other hand you can just use $json_array->data instead $json_array["data"].

Comments

0

Try This :

$json = '{"length":1847,"data":[0,0,0,0,-46,37]}';
$a = json_decode($json);
print_r($a->data);

Value after json_decode in variable $a is stored in the object form and you can not get the object value like $a['data'] so you have to use in this manner $a->data

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.