3v4l.org

run code in 300+ PHP versions simultaneously
<?php $obj = [ 'this' => 'that', 'and' => 'the', 'other' => (object) [ 'layers' => 'make', 'things' => 'more', 'fun' => (object) [ 'every' => 'time', 'foo' => 'bar', ], ], ]; // Just sort and return the sorted array function array_ksort(array $array) { ksort($array); return $array; } $obj = (object) array_ksort(array_map(function($value) { return !is_object($value) ? $value : (object) array_ksort((array) $value);}, $obj)); var_dump($obj);
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
object(stdClass)#3 (3) { ["and"]=> string(3) "the" ["other"]=> object(stdClass)#4 (3) { ["fun"]=> object(stdClass)#1 (2) { ["every"]=> string(4) "time" ["foo"]=> string(3) "bar" } ["layers"]=> string(4) "make" ["things"]=> string(4) "more" } ["this"]=> string(4) "that" }

preferences:
178.49 ms | 408 KiB | 5 Q