Press "Enter" to skip to content

Convert your deeply nested objects to array

Sharing is caring!

In php, you can convert nested object to array in many ways.

  • You can loop through the properties.
  • You can (array) $object to change.
  • Or create an entirely new one.

But things get messy with deeply nested object.

So, we have a little neat trick here.

To convert deeply nested objects to array, we can simply use json functions provided by php.

json_encode and json_decode are two of them.

Here, second parameter asks to turn your object to associative array.

Everything is awesome.


One Comment

Comments are closed, but trackbacks and pingbacks are open.