Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an array result from json_decode

Tags:

json

arrays

php

How do I get an array as a result from json_decode()?

I had an array like this:

$array = array(
  'mod_status' => 'yes',
  'mod_newsnum' => 5
);

and I saved this in database like JSON encode:

{"mod_status":"yes","mod_newsnum":5}

Now I want to get array again from database. But when i use:

$decode = json_decode($dbresult);

I get:

stdClass Object (
  [mod_status] => yes
  [mod_newsnum] => 5
)

Instead of an array. How can I get an array instead of an object?

like image 350
Shark Avatar asked Mar 06 '11 07:03

Shark


2 Answers

Set the second parameter of json_decode to true to force associative arrays:

$decode = json_decode($dbresult, true);
like image 74
Gumbo Avatar answered Nov 13 '22 05:11

Gumbo


As per http://in3.php.net/json_decode:

$decode = json_decode($dbresult, TRUE);
like image 22
Kumar Avatar answered Nov 13 '22 03:11

Kumar