Algorithm question.
All original property values of an object are in upper case. do you need to convert all property values of an object into lower case. The general idea is to convert an object into an array (traversal is converted to lowercase) the idea of re-converting to an Object is cumbersome and the recursive data source is inefficient. the idea of efficiency or method stdClass Object ([INFO] => stdClass Object ([A] => 1 [B] => 2 [C] => 3 [C] => 4 [D] => stdClass Object ([AA] => 11 [BB] => 22 [CC] => 33 [CC] => 44 )))
Reply to discussion (solution)
It is impossible for an object to have a property of the same name. Please adjust it.
It is impossible for an object to have a property of the same name. Please adjust it.
stdClass Object( [INFO] => stdClass Object ( [A] => 1 [B] => 2 [C] => 3 [D] => stdClass Object ( [AA] => 11 [BB] => 22 [CC] => 33 [CC] => 44 ) ) )
Do moderators have a good idea?
After being converted to an array, you can use the array iterator in spl to traverse it. I don't know if the speed will be faster.
I don't quite understand why I have such a requirement. I can write it like this.
$ S = '{"A": 1, "B": 2, "C": 3, "D": {"AA": 11, "BB": 22, "CC": 33} '; $ o = json_decode ($ s); // simulate an object $ t = serialize ($ o ); $ t = preg_replace_callback ('/("[A-Z] + ")(;. + ?;) /', Function ($ r) {return strtolower ($ r [1]). $ r [2] ;}, $ t); $ o = unserialize ($ t); print_r ($ o );
stdClass Object( [a] => 1 [b] => 2 [c] => 3 [d] => stdClass Object ( [aa] => 11 [bb] => 22 [cc] => 33 ))
If it is json, it can be written.
$s = '{"A":1,"B":2,"C":3,"D":{"AA":11,"BB":22,"CC":33}}';$s = preg_replace_callback('/"[A-Z]+":/', function($r) { return strtolower($r[0]); }, $s);print_r(json_decode($s));
stdClass Object( [a] => 1 [b] => 2 [c] => 3 [d] => stdClass Object ( [aa] => 11 [bb] => 22 [cc] => 33 ))
1. convert the object to a string using json_encode.
2. use preg_replace_callback to call strtolower to convert "*": to lowercase, which is equivalent to replacing traversal with regular expression.
3. json_decode converts a string into an object.