Recently in the use of thinkphp to develop an Jingdong service market application, but the Jingdong service market interface The data returned is an array of objects. However, you need to take a property out of the array and then use the thinkphp AddAll or Add method to write to the database. However, each return of the field has dozens of, each time such stitching will collapse. Sure enough, when you feel unbearable, you will find a way to change. So, if you have a function that passes an array of objects, you can automatically turn to an ordinary array. So the almighty Internet search has come again. Baidu has a pass ... Indeed, a senior has been dealt with, this record.
Copy Code code as follows:
/**
* [Std_class_object_to_array objects into groups]
* @param [Stdclass] $stdclassobject [objects]
* @return [array] [arrays]
*/
function Std_class_object_to_array ($stdclassobject)
{
$_array = Is_object ($stdclassobject)? Get_object_vars ($stdclassobject): $stdclassobject;
foreach ($_array as $key => $value) {
$value = (Is_array ($value) | | | is_object ($value))? Std_class_object_to_array ($value): $value;
$array [$key] = $value;
}
return $array;
}
In this way, the object array is gracefully turned into an ordinary array. The brain, the amount of code down, the function is also gracefully achieved. Why not kill the birds?