How to convert object into array in php

PHPz
Release: 2023-04-24 16:03:15
Original
604 people have browsed it

PHP is a popular server-side programming language that supports relational databases, is used to operate fonts, use HTML, and many other functions. In PHP, Object is used as a data type. Object is frequently used in different PHP extension libraries and frameworks. However, sometimes it is necessary to convert Object into an array for better data processing. In this article, we will explain how to convert Object into an array in PHP.

Object type in PHP

In PHP, the Object type is called a backing type, which is the top-level and most basic data type in PHP. Its type protocol can support class instances and non-class objects.

For example:

class Fruits
{
    public $apple_color = 'red';
    public $orange_color = 'orange';
    function __construct($apple_color, $orange_color) {
        $this->apple_color = $apple_color;
        $this->orange_color = $orange_color;
    }
}

$fruit_instance = new Fruits('green', 'yellow');
$fruit_object = (object)array('apple' => 'red', 'orange' => 'orange', 'furniture' => 'blue');
Copy after login

In the above code, Fruits is a class and we create an instance of it$fruit_instance and an object$fruit_object. They both belong to Object type data, but $fruit_instance is a "class instance", while $fruit_object is a "non-class object".

In PHP, how to convert Object into an array? The following lists several methods that can be used:

Method 1: Use type conversion

Forced type conversion in PHP can convert any type of object (Object) into an array. For example:

$array = (array)$fruit_object;
print_r($array);
Copy after login

When we run the above code, the output result is:

Array
(
    [apple] => red
    [orange] => orange
    [furniture] => blue
)
Copy after login
Copy after login
Copy after login
Copy after login

However, it should be noted that when we perform cast type conversion, Object will be converted into an array , but this is not the result we expect, because the information of the original data will be lost, and we will have to manually reprocess the data to retain the original structure.

Method 2: Use the get_object_vars function

The get_object_vars function in PHP can be used to obtain the attributes and attribute values ​​of the Object object in the parameter, and returns an array. For example:

$array = get_object_vars($fruit_object);
print_r($array);
Copy after login

When we run the above code, the output result is:

Array
(
    [apple] => red
    [orange] => orange
    [furniture] => blue
)
Copy after login
Copy after login
Copy after login
Copy after login

It should be noted that the get_object_vars function can only get the attributes of the object, there is no method , but we can get all properties through the member functions of a class.

Method 3: Use the json_decode function

The json_decode function in PHP can also convert a string in JSON format into an array. For example:

$json_str = json_encode($fruit_object);
$array = json_decode($json_str, true);
print_r($array);
Copy after login

When we run the above code, the output result is:

Array
(
    [apple] => red
    [orange] => orange
    [furniture] => blue
)
Copy after login
Copy after login
Copy after login
Copy after login

It should be noted that the json_decode function also supports the processing of binary data.

Method 4: Use a custom function

We can also customize a function to convert the Object object into an array. For example:

function object_to_array($object)
{
    if (is_object($object)) {
        // 获取对象的属性和属性值
        $array = get_object_vars($object);
    } elseif (is_array($object)) {
        foreach ($object as $key => $value) {
            $array[$key] = object_to_array($value);
        }
    } else {
        $array = $object;
    }
    return $array;
}

$array = object_to_array($fruit_object);
print_r($array);
Copy after login

When we run the above code, the output result is:

Array
(
    [apple] => red
    [orange] => orange
    [furniture] => blue
)
Copy after login
Copy after login
Copy after login
Copy after login

It should be noted that we need to make certain judgments and processing of different data types in order to make our Functions can handle different situations.

Summary

In PHP, the Object type has been widely used in different data applications. Converting Object into an array makes data processing more flexible and easier. However, when we convert Object, we need to flexibly choose different methods according to the actual situation in order to better retain the original data structure and information.

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