Home >Backend Development >PHP Tutorial >How to convert PHP object array into normal array?
How to convert PHP object array into a normal array?
When using the jQuery EasyUI framework for program development, the frontend passes JSON format data to the server backend, and the array converted by PHP's json_decode function is an object array, and the PHP program cannot process the data normally. , for this purpose you need to develop a PHP callback function (objarray_to_array) to convert the object array into a normal array.
Php code
/**
* Convert object array to ordinary array
*
* The JSON string submitted to the background by AJAX is decoded into an object array,
* For this reason, it must be converted into an ordinary array before subsequent processing,
* This function supports multi-dimensional array processing.
*
* @param array
* @return array
*/
function objarray_to_array($obj) {
$ret = array();
foreach ($obj as $key => $value) {
if (gettype($value) == "array" || gettype($value) == "object"){
$ret[$key] = objarray_to_array($value);
}else{
$ret[$key] = $value;
}
}
return $ret;
}