Home >Backend Development >PHP Problem >How to convert object to array in php
php method to convert an object into an array: 1. Get the object; 2. Define an empty array; 3. Loop through the object and assign a value to the array, such as [foreach ($obj as $key = > $value){$data[$key]=$value;}].
Ideas:
1. Get the object;
2. Define an empty array;
3. Loop through the objects and assign values to the array.
(Recommended tutorial: php video tutorial)
Code implementation:
// 获取对象 $obj = (object) array('1' => 'foo','2'=>'goo',3=>'hoo'); // 定义空数组 $data = array(); // 循环遍历对象,对数组赋值 foreach ($obj as $key => $value) { $data[$key] = $value; } // 打印数组 var_dump($data);
Result:
Related recommendations: php training
The above is the detailed content of How to convert object to array in php. For more information, please follow other related articles on the PHP Chinese website!