0

So here we have an array function array_values but it is working if performing on the same array. Over here i have an object and inside that objst i have set of arrays and with few condition i have to unset few of the keys and reindex the array.

stdClass Object
(
    [details] => stdClass Object
    (
        [first] => 159
        [events] => Array
        (
           [0] => stdClass Object
           (
               [id]=>1,
               [name]=>abc
           )
           [1] => stdClass Object
           (
               [id]=>2,
               [name]=>abc
           ) 
           [2] => stdClass Object
           (
               [id]=>3,
               [name]=>abc
           )
       )
   )
)

Now if i unset the 1 key and do json_encode and while printing it by doing json_decode, then it becomes

stdClass Object
(
    [details] => stdClass Object
    (
        [first] => 159
        [events] => stdClass Object
        (
           [0] => stdClass Object
           (
               [id]=>1,
               [name]=>abc
           )
           
           [2] => stdClass Object
           (
               [id]=>3,
               [name]=>abc
           )
       )
   )
)

The events node becomes object, I don't want to change it from array to object. My result has to be exactly the same with reindexing the key.

1
  • 2
    Store the result of array_values back into the object property. Commented May 11, 2021 at 19:25

1 Answer 1

1

Use array_values() to reindex an array.

$object->events = array_values($object->events);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. It did work for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.