Home Backend Development PHP Problem How to convert object to array in php

How to convert object to array in php

Apr 24, 2023 am 09:07 AM

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);
Copy after login

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
)
Copy after login

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);
Copy after login

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
)
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24