Home > Article > Backend Development > How to convert php array to object
The way to convert a php array into an object is to convert it directly by creating a new anonymous class. The specific statement is "protected function arrayTransitionObject(Array $array) {if (is_array($array)) }".
php array conversion object method
Although php cannot directly new Object like js, php supports anonymous classes We can just create a new anonymous class for conversion
/** * 数组转对象 * @param Array $array * @author Quan * @return Object */ protected function arrayTransitionObject(Array $array) { if (is_array($array)) { $obj = new class{}; foreach ($array as $key => $val) { $obj->$key = $val; } } else { $obj = $array; } return $obj; }
Recommended: "PHP Tutorial"
The above is the detailed content of How to convert php array to object. For more information, please follow other related articles on the PHP Chinese website!