0

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?

plasticheart
  • 57
  • 1
  • 6

1 Answers1

2

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

Nick
  • 138,499
  • 22
  • 57
  • 95
  • [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Oct 23 '22 at 07:49