In PHP, objects and arrays are two very important data types. Objects are generally used to represent entities and concepts in the real world, while arrays are used to store a set of related data. But in actual development, we may need to convert an object into an array, or convert an array into an object. This article will introduce methods and techniques for converting between objects and arrays in PHP.
1. Convert an object into an array
There are two main methods for converting an object into an array: automatic conversion and manual conversion.
In PHP, objects can be traversed like an array or printed directly, at which time PHP will automatically convert the object into an array. For example:
class Person { public $name = 'Jack'; private $age = 30; } $person = new Person(); print_r($person); // 输出结果:Person Object ( [name] => Jack )
In the above example, although we did not define the __toString()
method in the Person
class to specify the string that should be output when the object is printed, But PHP is still able to convert the object into an array containing the name
attribute. This is because PHP will save all the public
properties of the object as key-value pairs of the array, while the private
properties and protected
properties cannot be accessed.
In addition, if we traverse an object, PHP will automatically convert the object into an array. For example:
foreach ($person as $key => $value) { echo $key . ': ' . $value . PHP_EOL; }
At this time, PHP will automatically convert the $person
object into an array containing the name
attribute, and then output the key-value pair when traversing the array.
If we need to more precisely control the process of converting an object into an array, we can use the get_object_vars()
function to manually obtain the object All attributes, and then use the array()
function to convert them into an array. For example:
class Person { public $name = 'Jack'; private $age = 30; public function toArray() { return array( 'name' => $this->name, 'age' => $this->age ); } } $person = new Person(); print_r($person->toArray()); // 输出结果:Array ( [name] => Jack, [age] => 30 )
In the above example, we defined a toArray()
method, which returns an array containing name
and age
Array of properties. Here we can manually control which attributes need to be converted into arrays, and the key-value correspondence in the array.
2. Convert an array into an object
There are two main methods for converting an array into an object: array conversion and forced conversion.
When the key value of an array is the same as the attribute name of a class, we can convert the array into an instance object of the class.
class Person { public $name; public $age; } $data = array('name' => 'Jack', 'age' => 30); $person = (object)$data; echo $person->name; // 输出结果:'Jack'
In the above example, we use the key-value pairs in the $data
array to assign values to the attributes of the Person
class, and then convert the array into An instance object of the Person
class. Note that here we use the cast operator (object)
to convert the array into an object.
Of course, if we need to batch convert multiple arrays, we can also use a loop to perform the conversion.
class Person { public $name; public $age; } $dataList = array( array('name' => 'Jack', 'age' => 30), array('name' => 'Lucy', 'age' => 25) ); $personList = array(); foreach ($dataList as $data) { $personList[] = (object)$data; } echo $personList[0]->name; // 输出结果:'Jack' echo $personList[1]->name; // 输出结果:'Lucy'
In the above example, we used a loop to batch convert arrays into objects. Here we use the (object)
operator to convert an array into an object and add it to the $personList
array.
In addition to using the (object)
operator for conversion, we can also use forced type conversion to convert an array into an object. For example:
class Person { public $name; public $age; } $data = array('name' => 'Jack', 'age' => 30); $person = new Person(); $person->name = $data['name']; $person->age = $data['age']; echo $person->name; // 输出结果:'Jack'
In the above example, we first create an instance object of the Person
class, and then give it name
and age
Properties are assigned values separately. This method requires us to manually assign a value to each attribute, which seems a bit cumbersome. Therefore, you need to choose according to the specific situation when using it.
Summary
Objects and arrays are two very important data types in PHP. They are usually used to save and manipulate data. In actual development, we may convert an object into an array, or an array into an object. This article introduces the methods and techniques for converting objects and arrays in PHP, including automatic conversion and manual conversion. I hope this article can help everyone better master PHP programming skills and improve development efficiency.
The above is the detailed content of Convert php objects and arrays to each other. For more information, please follow other related articles on the PHP Chinese website!