PHP Array convert from object and class
From w3cyberlearnings
create php object
<?php $employee_obj = new stdClass(); $employee_obj->name = 'Jamy Jo'; $employee_obj->age = 58; $employee_obj->address->street = '240 Lake Rd'; $employee_obj->address->apartment = '240'; $employee_obj->address->state = 'Tx'; $employee_obj->address->city = 'Houston'; $employee_obj->address->zip = '77300'; $employee_obj->role = 'PHP Developer'; print_r($employee_obj); ?>
Display Result
stdClass Object ( [name] => Jamy Jo [age] => 58 [address] => stdClass Object ( [street] => 240 Lake Rd [apartment] => 240 [state] => Tx [city] => Houston [zip] => 77300 ) [role] => PHP Developer )
convert the php object to array
<?php $employee_obj = new stdClass(); $employee_obj->name = 'Jamy Jo'; $employee_obj->age = 58; $employee_obj->address->street = '240 Lake Rd'; $employee_obj->address->apartment = '240'; $employee_obj->address->state = 'Tx'; $employee_obj->address->city = 'Houston'; $employee_obj->address->zip = '77300'; $employee_obj->role = 'PHP Developer'; // convert to an array $my_array = (array) $employee_obj; print_r($my_array); ?>
Display Result
- The object converted to array, as you can see the address object is still PHP object.
- In order to convert all the objects within another object to array, you need to write a function to convert the object to array recursively.
Array ( [name] => Jamy Jo [age] => 58 [address] => stdClass Object ( [street] => 240 Lake Rd [apartment] => 240 [state] => Tx [city] => Houston [zip] => 77300 ) [role] => PHP Developer )
php function to convert the object to array recursively
function objectToArray($d) { if (is_object($d)) { // Gets the properties of the object $d = get_object_vars($d); } if (is_array($d)) { return array_map(__FUNCTION__, $d); } else { // Return array return $d; } }
return array from object recursively
<?php $employee_obj = new stdClass(); $employee_obj->name = 'Jamy Jo'; $employee_obj->age = 58; $employee_obj->address->street = '240 Lake Rd'; $employee_obj->address->apartment = '240'; $employee_obj->address->state = 'Tx'; $employee_obj->address->city = 'Houston'; $employee_obj->address->zip = '77300'; $employee_obj->role = 'PHP Developer'; print_r(objectToArray($employee_obj)); function objectToArray($d) { if (is_object($d)) { // Gets the properties of the object $d = get_object_vars($d); } if (is_array($d)) { return array_map(__FUNCTION__, $d); } else { // Return array return $d; } } ?>
Display Result
Array ( [name] => Jamy Jo [age] => 58 [address] => Array ( [street] => 240 Lake Rd [apartment] => 240 [state] => Tx [city] => Houston [zip] => 77300 ) [role] => PHP Developer )
from array to object with simple array
<?php $my_experience = array('work', 'life', 'happy'); $obj = (object) $my_experience; print_r($obj); ?>
Display Result
stdClass Object ( [0] => work [1] => life [2] => happy )
complex array or multiple array to object
<?php $my_experience = array( 'work', 'life', 'happy', 'happy2'=>array('go out','race car','programming'), 'programming'=>'php' ); $obj = (object) $my_experience; print_r($obj); ?>
display result
As you can see, multiple array is not converted to the object.
stdClass Object ( [0] => work [1] => life [2] => happy [happy2] => Array ( [0] => go out [1] => race car [2] => programming ) [programming] => php )
php function to convert multiple array to object recursively
<?php $my_experience = array( 'work', 'life', 'happy', 'happy2' => array('go out', 'race car', 'programming'), 'programming' => 'php' ); $obj = arrayToObject($my_experience); print_r($obj); function arrayToObject($d) { if (is_array($d)) { //recurse function return (object) array_map(__FUNCTION__, $d); } else { // Return object return $d; } } ?>
Display Result
stdClass Object ( [0] => work [1] => life [2] => happy [happy2] => stdClass Object ( [0] => go out [1] => race car [2] => programming ) [programming] => php )
php class
<?php class myinfo { public $name; public $age; public $salary; public function __construct($name, $age, $salary) { $this->name = $name; $this->age = $age; $this->salary = $salary; } public function display() { echo 'My name is ' . $this->name . '<br/>'; echo 'My age is ' . $this->age . '<br/>'; echo 'My salary is ' . $this->salary . '<br/>'; } } $mpersonal = new myinfo('Jammy', 24, 54000); print_r($mpersonal); ?>
Display Result
myinfo Object ( [name] => Jammy [age] => 24 [salary] => 54000 )
convert the php class to array
<?php class myinfo { public $name; public $age; public $salary; public function __construct($name, $age, $salary) { $this->name = $name; $this->age = $age; $this->salary = $salary; } public function display() { echo 'My name is ' . $this->name . '<br/>'; echo 'My age is ' . $this->age . '<br/>'; echo 'My salary is ' . $this->salary . '<br/>'; } } $mpersonal = new myinfo('Jammy', 24, 54000); // convert class to array $mypersonal_aa = (array)$mpersonal; print_r($mypersonal_aa); ?>
Display Result
Array ( [name] => Jammy [age] => 24 [salary] => 54000 )
how to convert php array to class
<?php class myclass { public function __construct($array_class) { foreach ($array_class as $key => $value) { $this->{$key} = $value; } } } $array_class = array( 'Basic'=>'PHP', 'Advanced'=>'Object Oriented in PHP', 'Business'=>'Business System' ); $computer_class = new myclass($array_class); echo $computer_class->Basic; // display: PHP echo $computer_class->Advanced; // display: Object Oriented in PHP ?>
a better method to convert array to class variable
<?php class MyClass { private $_myvariable; public function __construct($properties) { $this->_myvariable = $properties; } // magic methods! public function __set($property, $value) { return $this->_myvariable[$property] = $value; } public function __get($property) { return array_key_exists( $property, $this->_myvariable) ? $this->_myvariable[$property] : null ; } } $array_class = array( 'Basic'=>'PHP', 'Advanced'=>'Object Oriented in PHP', 'Business'=>'Business System'); $class1 = new MyClass($array_class); echo $class1->Basic; // display: PHP $class1->php_salary = 70000; echo $class1->php_salary ; // display: 70000 ?>