Discuss type conversion between objects and arrays in PHP

PHPz
Release: 2023-04-19 10:36:22
Original
586 people have browsed it

In PHP, objects can be converted to arrays. This type conversion is very common because many times we need to convert the properties in the object to an array for easy use. In this article, we will discuss type conversion between objects and arrays in PHP and introduce some commonly used methods.

Convert object to array

In PHP, use the get_object_vars() function to convert an object to an array. This function will return an array containing the object's properties and property values, where the keys are the property names and the values ​​are the property values. For example, we can define an object with multiple properties and then convert it to an array:

class Person {
    public $name = '';
    public $age = '';
    public $email = '';
}

$person = new Person();
$person->name = 'Jack';
$person->age = 25;
$person->email = 'jack@example.com';

$person_array = get_object_vars($person);

print_r($person_array);
Copy after login

The output is as follows:

Array
(
    [name] => Jack
    [age] => 25
    [email] => jack@example.com
)
Copy after login

As shown above, we create an Person object, and three properties are set: name, age and email. Then, we convert this object to an array using the get_object_vars() function.

Convert array to object

We can also convert an array to object. Use (object) type conversion in PHP to achieve this operation. This method works with associative arrays, where the array's keys will become the object's property names, and the array's values ​​will become the object's property values.

$data = array(
    'name' => 'Tom',
    'age'  => 22,
    'email' => 'tom@example.com'
);

$person = (object) $data;

print_r($person);
Copy after login

The output result is as follows:

stdClass Object
(
    [name] => Tom
    [age] => 22
    [email] => tom@example.com
)
Copy after login

As shown above, we created an associative array $data, and then used the (object) function to Convert it to an object. The object type obtained after conversion is stdClass, which is a built-in standard type.

Mixing arrays and objects

In PHP, arrays and objects can be converted to each other, and they can be used together. For example, we can convert an object to an array, modify some of its properties, and then convert it back to the original object. This is particularly useful in some cases, such as when we need to modify the JSON data returned by an API and then serialize it into JSON format and return it.

$data = '{"name":"Tom","age":22,"email":"tom@example.com"}';
$person = json_decode($data);

$person_array = get_object_vars($person);

$person_array['age'] = 23;

$person = (object) $person_array;

echo json_encode($person);
Copy after login

The output results are as follows:

{"name":"Tom","age":23,"email":"tom@example.com"}
Copy after login

As shown above, we first use the json_decode() function to convert the JSON format string into an object. We then convert the object into an array $person_array and modify the values ​​within it. Finally, we use the (object) type conversion to reconvert the modified array to an object, and the json_encode() function to serialize the object into JSON string format.

Conclusion

In PHP, converting objects and arrays is a very common type conversion. Use the get_object_vars() function to convert an object to an array, and use the (object) type conversion to convert an array to an object. This type conversion is very practical and can be used in many projects. If you need to convert objects and arrays in PHP, these methods should help you.

The above is the detailed content of Discuss type conversion between objects and arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!