Method to convert object array into ordinary array in PHP, php array_PHP tutorial

WBOY
Release: 2016-07-13 09:46:49
Original
926 people have browsed it

How to convert object array into ordinary array in PHP, php array

Recently, I am using ThinkPHP to develop an application for JD.com service market. However, the data returned by JD.com service market interface is Array of objects. However, you need to take out the attributes one by one and put them into the array and then use ThinkPHP's addAll or add method to write them to the database. However, there are dozens of fields returned each time, and each time the splicing is done, it will collapse. Sure enough, it's still the same sentence, when you feel that you can't stand it, you will find a way to change it. So I thought about it, if there was a function that could automatically convert an object array into a normal array. So the universal internet search is back. Baidu made a pass. . . Sure enough, some seniors have already dealt with it, so I will record it here.
Copy code The code is as follows:
/**
* [std_class_object_to_array Convert object to array]
* @param [stdclass] $stdclassobject [object]
* @return [array] [array]
*/
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 elegantly converted into an ordinary array. Using my brain, the code size was reduced and the functions were implemented elegantly. Kill two birds with one stone, why not?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1030144.htmlTechArticleMethods to convert object arrays into ordinary arrays in PHP. PHP arrays. Recently, I am using ThinkPHP to develop a Jingdong service market. Application, however, the data returned by the JD service market interface is an object...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template