Home >Backend Development >PHP Problem >How to force an object to an array in php
php method to force an object to an array: 1. Convert via the defined "object_array" method; 2. Convert via the "json_decode" method; 3. Convert via the defined "object2array_pre" method.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php object to array
1.
//PHP stdClass Object转array function object_array($array) { if(is_object($array)) { $array = (array)$array; } if(is_array($array)) { foreach($array as $key=>$value) { $array[$key] = object_array($value); } } return $array; }
2.
$array = json_decode(json_encode(simplexml_load_string($xmlString)),TRUE);
3.
function object2array_pre(&$object) { if (is_object($object)) { $arr = (array)($object); } else { $arr = &$object; } if (is_array($arr)) { foreach($arr as $varName => $varValue){ $arr[$varName] = $this->object2array($varValue); } } return $arr; }
4. If the amount of data is 10W, the execution will take 1 second, and the structure will It is more complicated and can reach 3s. The performance is too poor. You can replace it with the following:
function object2array(&$object) { $object = json_decode( json_encode( $object),true); return $object; }
json_decode(json_encode($array),true)
The purpose of multi-layer array and object conversion is very simple, which is convenient for processing multi-layer numbers in WebService Conversion of groups and objects [Recommended study: "PHP Video Tutorial"]
Simple (array) and (object) can only process single-layer data. For multi-layer arrays and Object conversion does nothing.
Through json_decode(json_encode($object), the object can be converted into an array at one time, but problems will occur when encountering non-utf-8 encoded non-ascii characters in the object, such as gbk Chinese, not to mention json_encode and The performance of decode is also questionable.
The code below:
<?php function objectToArray($d) { if (is_object($d)) { // Gets the properties of the given object // with get_object_vars function $d = get_object_vars($d); } if (is_array($d)) { /* * Return array converted to object * Using __FUNCTION__ (Magic constant) * for recursive call */ return array_map(__FUNCTION__, $d); } else { // Return array return $d; } } function arrayToObject($d) { if (is_array($d)) { /* * Return array converted to object * Using __FUNCTION__ (Magic constant) * for recursive call */ return (object) array_map(__FUNCTION__, $d); } else { // Return object return $d; } } // Useage: // Create new stdClass Object $init = new stdClass; // Add some test data $init->foo = "Test data"; $init->bar = new stdClass; $init->bar->baaz = "Testing"; $init->bar->fooz = new stdClass; $init->bar->fooz->baz = "Testing again"; $init->foox = "Just test"; // Convert array to object and then object back to array $array = objectToArray($init); $object = arrayToObject($array); // Print objects and array print_r($init); echo "\n"; print_r($array); echo "\n"; print_r($object); ?>
The above is the detailed content of How to force an object to an array in php. For more information, please follow other related articles on the PHP Chinese website!