Algorithm question.

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

 

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.