phparraysobjectmultidimensional-arraystdclass

How can I do a reverse of json_decode(json_encode($), true)?


I have an array of objects, where I have an array with stdClass Object, here is ok, next I show a snipplet of array

Array
(
    [0] => stdClass Object
        (
            [ID] => 5384
            [post_title] => Accordeon me con jQuery
            [post_name] => accordeon-con-jquery
            [readings] => 55140
            [post_date] => 01-12-2012
            [media] => 59.546436285097
            [dias] => 926
        )
 
    [1] => stdClass Object
        (
            [ID] => 2509
            [post_title] => Popup me con jQuery
            [post_name] => popup-me-con-jquery
            [readings] => 26261
            [post_date] => 13-04-2012
            [media] => 22.677892918826
            [dias] => 1158
        )
        ...

Next I need convert to array multidimensional, I use this to get an array multidimensional

$visits = json_decode(json_encode($visits), true);

And I get this, and here is ok

Array
(
    [0] => Array
        (
            [ID] => 5384
            [post_title] => Accordeon me con jQuery
            [post_name] => accordeon-con-jquery
            [readings] => 55140
            [post_date] => 01-12-2012
            [media] => 59.546436285097
            [dias] => 926
        )
 
    [1] => Array
        (
            [ID] => 2509
            [post_title] => Popup me con jQuery
            [post_name] => popup-me-con-jquery
            [readings] => 26261
            [post_date] => 13-04-2012
            [media] => 22.677892918826
            [dias] => 1158
        )
        ...

So I need again an array of objects, so I use this

$visits = json_decode(json_encode($visits));

but I get an unexpected result, I get

stdClass Object
(
    [0] => stdClass Object
        (
            [ID] => 5384
            [post_title] => Accordeon me con jQuery
            [post_name] => accordeon-con-jquery
            [readings] => 55140
            [post_date] => 01-12-2012
            [media] => 59.546436285097
            [dias] => 926
        )
 
    [1] => stdClass Object
        (
            [ID] => 2509
            [post_title] => Popup me con jQuery
            [post_name] => popup-me-con-jquery
            [readings] => 26261
            [post_date] => 13-04-2012
            [media] => 22.677892918826
            [dias] => 1158
        )
        ...

What I can do to get again array - stdClass Object instead stdClass Object - stdClass Object?


Solution

  • Your problem is that you have non-sequential keys in the array, which causes json_encode to encode it as an object instead of an array (see example #4 on the manual page). If you don't need to preserve the keys, you can simply use array_values on $visits first:

    $visits = json_decode(json_encode(array_values($visits)));
    

    If you need to preserve keys, then just cast the result to an array:

    $visits = json_decode(json_encode($visits));
    $visits = (array)$visits;
    

    Demo on 3v4l.org