1$person = new stdClass();
2$person->firstName = "Taylor";
3$person->age = 32;
4
5//Convert Single-Dimention Object to array
6$personArray = (array) $person;
7
8//Convert Multi-Dimentional Object to Array
9$personArray = objectToArray($person);
10function objectToArray ($object) {
11 if(!is_object($object) && !is_array($object)){
12 return $object;
13 }
14 return array_map('objectToArray', (array) $object);
15}
1// Very fast way (More at pereere.com/php)
2$obj = json_decode( json_encode( $obj ), true );
3
4
5// Recursive Method
6// Dont create function if it already exist
7 if ( ! function_exists( 'convert_object_to_array' ) ) {
8
9 /**
10 * Converts an object to an array recursively.
11 *
12 * @param object $obj The object to convert
13 * @param array $arr Recursive
14 *
15 * @return mixed
16 *
17 */
18 function convert_object_to_array( $obj, &$arr = [] ) {
19
20 // Return if @var $obj is not an object (may be an array)
21 if ( ! is_object( $obj ) && ! is_array( $obj ) ) {
22 $arr = $obj;
23
24 return $arr;
25 }
26
27 foreach ( $obj as $key => $value ) {
28 if ( ! empty( $value ) ) {
29 $arr[ $key ] = array();
30 // Recursively get convert the value to array as well
31 convert_object_to_array( $value, $arr[ $key ] );
32 } else {
33 $arr[ $key ] = $value;
34 }
35 }
36
37 return $arr;
38 }
39 }