So lets say I have this:
<?php
$obj=(object)array("this"=>"that","and"=>"the","other"=>(object)array("layers"=>"make","things"=>"more","fun"=>(object)array("every"=>"time","foo"=>"bar")));
var_dump($obj);
?>
which outputs this:
object(stdClass)#3 (3) {
["this"]=>
string(4) "that"
["and"]=>
string(3) "the"
["other"]=>
object(stdClass)#2 (3) {
["layers"]=>
string(4) "make"
["things"]=>
string(4) "more"
["fun"]=>
object(stdClass)#1 (2) {
["every"]=>
string(4) "time"
["foo"]=>
string(3) "bar"
}
}
}
But I want it sorted by keys like this preferably without having to make a array or ever layer of keys and sort it and rebuilding the object in order like this:
object(stdClass)#3 (3) {
["and"]=>
string(4) "the"
["other"]=>
object(stdClass)#2 (3) {
["fun"]=>
object(stdClass)#1 (2) {
["every"]=>
string(4) "time"
["foo"]=>
string(3) "bar"
}
["layers"]=>
string(4) "make"
["things"]=>
string(4) "more"
}
["this"]=>
string(3) "that"
}
I think web browsers will do this sorting automatically as I seem to recall having to workaround this when I had a custom sorting feature I made break some years ago in firfox, so I could just json_encode
it and let the client handle this part so the browser will just sort it with no sorting effort on my part