Algorithm problem.
The idea is to convert an object into an array (traverse into lowercase) and convert it into an object. This is a cumbersome recursive data source and inefficient
To seek an efficient idea 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
)
)
)
------Solution--------------------
Object cannot appear with the same Name property, please adjust
------Solution--------------------
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
------Solution--------------------
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 ': 33} ';
$o = Json_decode ($s); Simulating 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's JSON, you can write.
$s = ' {' A ': 1, ' B ': 2, ' C ': 3, ' D ': {' AA ': One, ' BB ': ', ' 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
)
)
------Solution--------------------
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.
$s = ' {' A ': 1, ' B ': 2, ' C ': 3, ' D ': {' AA ': One, ' BB ': ', ' CC ': 33} ';
$obj = Json_decode ($s);
Print_r ($obj);
$str = Json_encode ($obj);
$str = Preg_replace_callback ('/' (. *?) ":/', ' ToLower ', $str);
$obj = Json_decode ($STR);
Print_r ($obj);
function ToLower ($matches) {
Return ' "'. Strtolower ($matches [1]). '": ';
}
?>