The method of converting object array into normal array in PHP _php instance

Source: Internet
Author: User

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?

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.