Home  >  Article  >  Backend Development  >  How to convert object to array in php

How to convert object to array in php

PHPz
PHPzOriginal
2023-04-24 09:07:13688browse

In PHP programming, we usually use objects to store and process data. However, in some cases, we need to convert the object to an array for processing.

In PHP, you can use the get_object_vars() function to convert an object into an array. This function takes one parameter, the object to be converted to an array.

The following is an example:

class Person {
    public $name = 'Tom';
    public $age = 25;
    private $email = 'tom@email.com';
}

$person = new Person();
$personArray = get_object_vars($person);
print_r($personArray);

In this example, we define a class named Person and define three attributes in it: public$name and $age, and the private $email attribute. We then instantiated the Person class and passed it to the get_object_vars() function to convert it to an array. Finally, we print out the personArray array.

The output result is as follows:

Array
(
    [name] => Tom
    [age] => 25
)

It can be seen that only the public attributes are converted into arrays, and the private attributes $email are not included in the array.

If we want to include private properties, we can use the ReflectionClass class. This class allows us to access and modify the private properties and methods of the class.

The following is an example:

class Person {
    public $name = 'Tom';
    public $age = 25;
    private $email = 'tom@email.com';
}

$person = new Person();
$reflector = new ReflectionClass($person);
$properties = $reflector->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PRIVATE);
$personArray = array();
foreach ($properties as $property) {
    $property->setAccessible(true);
    $personArray[$property->getName()] = $property->getValue($person);
}
print_r($personArray);

In this example, we use the ReflectionClass class to obtain class information. We pass an instance of the Person class to the ReflectionClass constructor and then use the getProperties() method to get the properties of the class using ReflectionProperty::IS_PUBLIC and ReflectionProperty::IS_PRIVATE parameters to include all public and private properties. Next, we set each private property to accessible using the setAccessible() method and get the value of each property using the getValue() method. Finally, we store these properties and values ​​in the $personArray array and print the output.

The output result is as follows:

Array
(
    [name] => Tom
    [age] => 25
    [email] => tom@email.com
)

It can be seen that all attributes including the private attribute $email are converted into arrays.

Summary:

Use the get_object_vars() function to convert an object into an array, but only contains public properties. If you need to include private properties, you can use the ReflectionClass class and use the setAccessible() method to set the private properties to an accessible state, and then use the getValue() method to get the value of the private property.

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!

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