Algorithm problem.

Source: Internet
Author: User
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.

 
  
  • 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.