Home php Can't translate stdClass Object to array

Can’t translate stdClass Object to array

Author

Date

Category

Given:

$ array = 'Array
(
[0] = & gt; stdClass Object
  (
    [did] = & gt; 123
    [owner_id] = & gt; 123123
    [title] = & gt; By
    [size] = & gt; 2061840
    [ext] = & gt; gif
    [url] = & gt; http://vk.com/
    [thumb] = & gt; http://cs610521.vk.me
    [thumb_s] = & gt; http: //cs610521.vk
  )
) ';

We need to translate this miracle into a regular array. Nothing works. Tried it like this, nothing comes out:

print_r ($ array [0] - & gt; did);

Answer 1, authority 100%

On this line, you declare LINE:

$ array = 'Array
(
[0] = & gt; stdClass Object
  (
    [did] = & gt; 123
    [owner_id] = & gt; 123123
    [title] = & gt; By
    [size] = & gt; 2061840
    [ext] = & gt; gif
    [url] = & gt; http://vk.com/
    [thumb] = & gt; http://cs610521.vk.me
    [thumb_s] = & gt; http: //cs610521.vk
  )
) ';

If you want an object, do something like this:

$ obj = new stdClass ();
$ obj- & gt; did = 123;
$ obj- & gt; title = 'asd';

Do you want it to be in an array –

$ array = [$ obj, $ obj1, $ obj2];

then and

print_r ($ array [0] - & gt; did);

will work.

upd

You claim that you are working with the vk api, and so, it returns valid json \ xml.
An example of working with json

$ array = json_decode ($ apiResponse); // $ apiResponse - the response of the VK server in json format
$ array [0] - & gt; did // exactly what you wanted!

And if you do

var_dump ($ array);

ps read the documentation. var_dump () print_r ()


Answer 2, authority 50%

$ data = '[{"xx": "1"}]';
$ array = json_decode ($ data, true);
print_r ($ array);

result:

Array
(
  [0] = & gt; Array
    (
      [xx] = & gt; 1
    )
)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions