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

php convert object to array

WBOY
WBOYOriginal
2023-05-23 14:01:16489browse

In PHP, objects and arrays are two basic data types. Objects are an important part of object-oriented programming (OOP) languages, which allow us to combine data and functions to make them more convenient to use. In contrast, an array is an unordered collection that can hold many different types of data. In some cases, we may need to convert objects into arrays to perform some operations or facilitate output, so this article will focus on how to convert objects into arrays in PHP.

Generally speaking, PHP provides two ways to convert objects into arrays, namely using cast and using PHP built-in functions. The following are explained separately:

1. Use forced type conversion

Forced type conversion in PHP is very simple. You only need to place the value to be forced before the target data type to be converted. . The same goes for converting objects to arrays. The following is a sample code to cast an object to an array:

$obj = new stdClass();
$obj->foo = 'bar';
$obj->hello = 'world';

$arr = (array) $obj;

print_r($arr);

Output result:

Array
(
    [foo] => bar
    [hello] => world
)

As can be seen from the above code, by casting an object to an array, we can easily The properties of the object are converted into the key-value pair format of the array.

2. Use PHP built-in functions

Compared with forced type conversion, using PHP built-in functions to convert objects to arrays may be more convenient in some cases. PHP provides a get_object_vars() function, which is used to obtain all non-static member variables of the specified object. We can convert the object into an array by directly assigning the value returned by this function to the array. The following is a sample code that uses this method to convert an object to an array:

class Person {
    public $name;
    public $age;
    private $gender;
    
    public function __construct($name, $age, $gender) {
        $this->name = $name;
        $this->age = $age;
        $this->gender = $gender;
    }
}

$obj = new Person('Alice', 25, 'female');
$arr = get_object_vars($obj);

print_r($arr);

Output result:

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

As can be seen from the above code, use the get_object_vars() function Converting an object to an array will only return the public properties of the object, while private and protected properties will not be returned.

It should be noted that when there are undefined attributes in the object, using the get_object_vars() function will throw a traditional warning. Therefore, when using this method, you should first check whether the object has this property defined. Alternatively, you can also use the json_decode(json_encode($object), true) method, which uses the get_object_vars() function internally and is more rigorous in handling undefined attributes. . The following is a sample code that uses this method to convert an object to an array:

$obj = new stdClass();
$obj->foo = 'bar';
$obj->hello = 'world';

$arr = json_decode(json_encode($obj), true);

print_r($arr);

Output result:

Array
(
    [foo] => bar
    [hello] => world
)

In summary, converting a PHP object to an array can either use cast or You can use PHP built-in functions, all of which can easily convert the properties in the object into the format of array key-value pairs. No matter which method is used, you need to pay attention to whether the defined properties are public properties and whether there are checks for undefined properties.

The above is the detailed content of php convert object to array. 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