Algorithm problem. How to solve it

Source: Internet
Author: User
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]). '": ';
}
?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.