0

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);

3 Answers 3

1

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

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

6 Comments

Hello, I do not wont to decode. I#am looking for the array to encode, witch looks like the encoded example.
What exactly do you want? To get array from json, or the opposite? You said "If I use Json_encode to this, I will get this:", but that code you can get only with json_decode, not encode.
I would like to get the origin array of my example above.
Then use what I wrote you already. Json is encoded data. Process of getting it back to array is decoding.
but is in the example above not the same. I have do create exactly this json output and do not know how.
|
1

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;

4 Comments

thx, but how does the array looks like to get this? if I do soemthing like this: $stack[1][org_src] = "Hallo"; $stack[1][src] = "scr"; the result is different $stack[2][org_src] = "jhhgh"; $stack[2][src] = "123";
Use the -> operator to access object properties. Like this: $stack[2]->src
do not get it. Example with $stack[2]->src??
"You asked for an example how to access stdClass's properties" No, I would like to create one.
0

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.

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.