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 );