Home > Backend Development > PHP Problem > PHP return object converted to array

PHP return object converted to array

WBOY
Release: 2023-05-19 14:33:08
Original
607 people have browsed it

In PHP programming, we often need to convert an object into an array. This requirement is usually because when we write web applications, the data obtained from the database or other data sources is usually returned in the form of objects. In order to handle this data and present it in a way that is easy to read and process, we need to convert these objects into arrays.

PHP is known for its flexibility and ease of use, so converting objects to arrays is also very simple. In PHP, we can use some built-in functions to accomplish this task, and we can also use third-party libraries to enhance the customizability and functionality of the conversion.

In this article, we will explore several ways to convert objects returned by PHP into arrays. In these methods, we will use PHP 7 syntax. However, you can easily adapt the syntax in these example codes to earlier versions of PHP.

  1. Use type casting (type coercion)

First, let us introduce the simplest way - use type casting (type coercion). This method is the simplest way to convert an object into an array, and it is also the most common way to use it.

When using type casting, we only need to surround the object with an array keyword. For example:

<?php
// a sample object
$obj = (object) ['foo' => 'bar', 'property' => 1];

// using type casting to convert object to array
$array = (array) $obj;

// displaying the array
print_r($array);
?>
Copy after login

The output is as follows:

Array
(
    [foo] => bar
    [property] => 1
)
Copy after login
Copy after login

As you can see, we can use type casting (type cast) to convert the object $obj to the array $array. Once the conversion is complete, we can use the print_r() function to view the contents of the array.

  1. Use the get_object_vars() function to convert

Secondly, let us use a built-in shortcut in PHP - the get_object_vars() function. This function returns an associative array consisting of object attributes named as keys and attribute values ​​as values.

When using this method, we only need to operate the object through the get_object_vars() function and store its results in an array. For example:

<?php
// a sample object
$obj = (object) ['foo' => 'bar', 'property' => 1];

// using get_object_vars() function to convert object to array
$array = get_object_vars($obj);

// displaying the array
print_r($array);
?>
Copy after login

The output is:

Array
(
    [foo] => bar
    [property] => 1
)
Copy after login
Copy after login

As you can see, we pass the object $obj to the get_object_vars() function and store its result in the array $array. In this example, the function returns the same result used in the type casting example.

  1. Use serialize() and unserialize() function conversion

Third, if the object is more complex, or we want to merge multiple objects into an array, then we This can be easily achieved using the serialize() and unserialize() functions. The serialization function serialize() can serialize an object into a string, and the unserialize() function can deserialize the string into an object.

When using this method, we need to serialize the object first to be able to convert it to a string and store it in an array. For example:

<?php
// a sample object
$obj1 = (object) ['foo' => 'bar', 'property' => 1];
$obj2 = (object) ['abc' => 'def', 'number' => 100];

// serialize objects to string, and then concatenate two strings into one
$str = serialize($obj1) . serialize($obj2);

// unserialize the concatenated string to array
$array = unserialize($str);

// displaying the array
print_r($array);
?>
Copy after login

The output is as follows:

Array
(
    [foo] => bar
    [property] => 1
    [abc] => def
    [number] => 100
)
Copy after login

As you can see, we have created two objects $obj1 and $obj2 in this example and serialized them into characters string. Next, we concatenate the two strings using the dot operator and pass this concatenated string to the unserialize() function to deserialize it into an array $array. This array will contain all properties and values ​​of both objects.

Conclusion

In this article, we learned three ways to convert the object returned by PHP into an array: type casting, conversion using the get_object_vars() function and serialize(), unserialize () function conversion. The use of these methods depends on your specific needs. If you encounter complex objects or have multiple objects that need to be converted, using the serialize() and unserialize() functions may be a better choice. However, for simple objects, it may be simpler to use a type cast or the get_object_vars() function.

The above is the detailed content of PHP return object converted to array. 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