Php deep learning Note 1 (conversion between arrays and objects)-php Tutorial

Source: Internet
Author: User
Php deep learning Note 1 (mutual conversion between arrays and objects) 1. object traversal $ objnewstdClass (); $ obj-& gt; name & quot; lihua & quot; $ obj-& gt; sex & quot; nan & quot ;; $ obj-& gt; age12; $ obj-& gt; address & quot; libaiinhangh php deep learning Note 1 (mutual conversion between arrays and objects)
// 1. object traversal $ obj = new stdClass (); $ obj-> name = "lihua"; $ obj-> sex = "nan"; $ obj-> age = 12; $ obj-> address = "lihua in hanghzou! "; Foreach ($ obj as $ v) {echo $ v ."
";}/* Lihua nan 12 lihua in hanghzou! * // 2. object to array // 2. 1. the first method is $ user = (array) $ obj; var_dump ($ user); // 2. 2. the second method // simple and violent conversion is only valid for the public attribute // The second parameter is true to return an array // false to return the object $ array = json_decode (json_encode ($ nested_object), true); // 3. convert an array to an object (note: associating an array) $ user = (object) $ user; var_dump ($ user );

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.