3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ObjectToArrayConverter { /** * @param mixed $value * * @raturn array */ public static function toArray($value) { if (! (is_object($value) || is_array($value))) { return $value; } if (is_object($value) && method_exists($value, '__toArray')) { return $value->__toArray(); } return array_map(__METHOD__, (array) $value); } } class Baz { public function __toArray() { return ['I R ARRAY']; } } $obj1 = new stdClass(); $obj2 = new stdClass(); $obj1->foo = 'foo'; $obj1->someArr = []; $obj1->obj2 = $obj2; $obj2->bar = 'bar'; $obj2->baz = new Baz(); var_dump(ObjectToArrayConverter::toArray($obj1));
Output for git.master, git.master_jit, rfc.property-hooks
array(3) { ["foo"]=> string(3) "foo" ["someArr"]=> array(0) { } ["obj2"]=> array(2) { ["bar"]=> string(3) "bar" ["baz"]=> array(1) { [0]=> string(9) "I R ARRAY" } } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
43.9 ms | 401 KiB | 8 Q