hamburger
hamburger

Reputation: 1435

json_encode how to get the orginal array [stdClass Object]

Hello I have this json:

   [{"org_src":"img\/base\/logo.png","src":"\/cache\/300-logo.png"},

    {"org_src":"\/img\/l2.JPG","src":"\/cache\/6l-2.JPG"},

    {"org_src":"\/img\/studio\/desk.JPG","src":"\/cache\/desk.JPG"}, 

    ...

How looks the array before its is decoded? If I use Json_encode to this, I will get this:

    Array
(
    [0] => stdClass Object
        (
            [og_src] => img/base/logo.png
            [src] => /cache/300-logo.png
        )

    [1] => stdClass Object
        (
            [og_src] => /img/l2.JPG
            [src] => /cache/6l-2.JPG
        )...

What is stdClass Object? Thanks for any help in advance.

with json_decode($test,true); I will get this:

Array
(
    [0] => Array
        (
            [og_src] => img/base/logo.png
            [src] => /cache/logo.png
        )

    [1] => Array
        (
            [og_src] => /img/studio/l2.JPG
            [src] => /cache/6l-2.JPG
...

This do not help me to see how the origin array looks like.

Here is the answer. That what I looked for. Thanks for any suggestion.

$stack[0][org_src]= "Hallo";
$stack[0][src] = "scrkjh";

$stack[1][org_src] = "Halfgfglo";
$stack[1][src] = "scrkjh";
json_encode($stack);

Upvotes: 0

Views: 1850

Answers (3)

hek2mgl
hek2mgl

Reputation: 157992

stdClass is the base class of all objects in PHP. If functions like json_decode() create objects that are not of a special class type or other data types will be casted to object, stdClass is used as the data type.

You can compare it to the Object data type in Java.


You asked for an example how to access stdClass's properties, or in general, object properties in PHP. Use the -> operator, like this:

$result = json_decode($json);

// access 'src' property of first result:
$src = $result[0]->src;

Upvotes: 1

William Buttlicker
William Buttlicker

Reputation: 6000

stdClass is php's generic empty class, kind of like Object in Java or object in Python

It is useful for anonymous objects, dynamic properties, etc. and Stdclass Object is its object

See Dynamic Properties in PHP and StdClass for example

Also.. As Marko D said, You can use json_decode($data, true); true to get an associative array instead of object.

Upvotes: 0

Marko D
Marko D

Reputation: 7576

You probably meant json_decode

If you look in the docs, you will notice that there is assoc parameter if you want to get associative array instead of an object.

So you should do

$data = json_decode($data, true);

if you don't want objects

Upvotes: 1

Related Questions