9

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?

Shalva Kakauridze
  • 1,225
  • 4
  • 12
  • 23
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Duncanmoo Dec 06 '18 at 06:50

5 Answers5

26

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

$decode = json_decode($dbresult, true);
Gumbo
  • 643,351
  • 109
  • 780
  • 844
7

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

$decode = json_decode($dbresult, TRUE);
dnch
  • 9,565
  • 2
  • 38
  • 41
Kumar
  • 5,038
  • 7
  • 39
  • 51
3
$decode = json_decode($dbresult, true);

Or

$decode = (array)json_decode($dbresult);
Gaurav
  • 28,447
  • 8
  • 50
  • 80
1

Casting the object result of json_decode to an array can have unexpected results (and cause headaches). Because of this, it is recommended to use json_decode($json, true) instead of (array)json_decode($json). Here is an example:

Broken:

<?php

$json = '{"14":"29","15":"30"}';
$data = json_decode($json);
$data = (array)$data;

// Array ( [14] => 29 [15] => 30 )
print_r($data);

// Array ( [0] => 14 [1] => 15 )
print_r(array_keys($data));

// all of these fail
echo $data["14"];
echo $data[14];
echo $data['14'];

// this also fails
foreach(array_keys($data) as $key) {
    echo $data[$key];
}

Working:

<?php

$json = '{"14":"29","15":"30"}';
$data = json_decode($json, true);

// Array ( [14] => 29 [15] => 30 )
print_r($data);

// Array ( [0] => 14 [1] => 15 )
print_r(array_keys($data));

// all of these work
echo $data["14"];
echo $data[14];
echo $data['14'];

// this also works
foreach(array_keys($data) as $key) {
    echo $data[$key];
}
AlbinoDrought
  • 986
  • 17
  • 24
0

If you only use that data in PHP I recommend using serialize and unserialize instead or else you'll never be able to differentiate between objects and associative arrays, because the object class information is lost when encoding to JSON.

<?php
class myClass{// this information will be lost when JSON encoding //
    public function myMethod(){
        echo 'Hello there!';
    }
}
$x = array('a'=>1, 'b'=>2);
$y = new myClass;
$y->a = 1;
$y->b = 2;
echo json_encode($x), "\n", json_encode($y); // identical
echo "\n", serialize($x), "\n", serialize($y); // not identical
?>

Run it.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
  • Doesn't answer question: should be comment instead. –  Mar 06 '11 at 07:51
  • 1
    @Mark It proposes an alternative that may be better. I consider those kind of answers valid as well. I added some more arguments in the latest edit. – Alin Purcaru Mar 06 '11 at 08:02