The original property value of the object is all uppercase, and the requirement is to convert all of the object's attribute values to lowercase. The idea is to convert an object into an array (traverse into lowercase) and then turn it into an object. The idea of a very cumbersome recursive data source and low efficiency for efficient thinking or method StdClass object ( [INFO] = StdClass Object ( [A] = 1 [B] = 2 [c] = 3 [c] = 4 [D] = = StdClass Object ( [AA] + = [ BB] = [ cc] = [ cc] = ()))
Reply to discussion (solution)
Object cannot appear with the same Name property, please adjust
Object cannot appear with the same Name property, please adjust
StdClass object ( [INFO] = = StdClass Object ( [A] = 1 [B] = 2 [C] = + 3 [D] = = StdC Lass Object ( [AA] + = [BB] [CC] = [ cc] = (+)]))
Does the moderator have a good idea?
After the conversion to an array, you can use the arrays iterator in SPL to traverse, do not know if the speed will be faster
It's not clear why there's a need for this, so to write
$s = ' {"A": 1, "B": 2, "C": 3, "D": {"AA": One, "BB": "CC": "Json_decode}} '; $o = ($s); Simulates an object $t = Serialize ($o); $t = Preg_replace_callback ('/("[a-z]+") (;. +?;) /', function ($r) {return Strtolower ($r [1]). $r [2];}, $t); $o = Unserialize ($t);p rint_r ($o);
StdClass object ( [A] = 1 [b] = 2 [c] = 3 [d] = = StdClass object ([AA] = 11
[BB] ([cc] =) )
If it's JSON, you can write.
$s = ' {"A": 1, "B": 2, "C": 3, "D": {"AA": One, "BB": "CC":-"}} '; $s = preg_replace_callback ('/" [a-z]+ ":/', function ($r) { return Strtolower ($r [0]); }, $s);p rint_r (Json_decode ($s));
StdClass object ( [A] = 1 [b] = 2 [c] = 3 [d] = = StdClass object ([AA] = 1 1 [BB] ([ cc] = ))
1. First json_encode the object to a string
2. Use Preg_replace_callback call Strtolower to turn "*": lowercase, equivalent to change the traversal to regular replacement.
3.json_decode to convert a string to an object.