Home  >  Article  >  Backend Development  >  How to convert PHP object array into normal array?

How to convert PHP object array into normal array?

巴扎黑
巴扎黑Original
2016-11-12 14:45:351131browse

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;

}


Statement:
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