Home > Backend Development > PHP Problem > How to convert php object into array

How to convert php object into array

王林
Release: 2023-05-11 09:35:36
Original
440 people have browsed it

In PHP programming, objects and arrays are two common data types. Sometimes, we need to convert a PHP object into an array for easy processing and operation. In this article, we will introduce how to convert a PHP object into an array.

First, we need to understand the structure of objects in PHP. A PHP object includes properties and methods. Properties can be public, private, or protected, while methods can be public, private, or protected. Access permissions for properties and methods determine whether they can be called by external code. In actual programming, we usually define a constructor in the class to initialize the properties of the object.

Before converting a PHP object to an array, we need to determine whether the object to be converted has a public property list. If not, we can consider storing the object properties and property values ​​in an associative array. You can easily obtain the public property list of a PHP object using PHP's Reflection API. All properties in a class can be obtained by calling the getProperties() method in the ReflectionClass class and return an array of ReflectionProperty objects. The ReflectionProperty object provides functions such as access to property names, property values, and access permissions to access properties. The following is a sample code:

class MyClass {
    public $name;
    protected $age;
    private $gender;

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

$obj = new MyClass('Tom', 22, 'Male');
$reflection = new ReflectionClass($obj);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
$arr = array();
foreach ($properties as $prop) {
    $prop_name = $prop->getName();
    $prop_value = $prop->getValue($obj);
    $arr[$prop_name] = $prop_value;
}

print_r($arr);
Copy after login

The output is as follows:

Array
(
    [name] => Tom
)
Copy after login

In this example, we define a MyClass class and create an instance $obj. Get the public property list of the $obj object through the ReflectionClass class and store the result in an array.

Next, we can consider converting the entire PHP object into an associative array. Here we can achieve this with the help of PHP's magic method __toArray(). The __toArray() method can return all attributes and attribute values ​​of a class in the form of an associative array. We can define this magic method in the class so that it returns an associative array containing all properties and property values. The following is a sample code:

class MyClass {
    public $name;
    protected $age;
    private $gender;

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

    public function __toArray() {
        return get_object_vars($this);
    }
}

$obj = new MyClass('Tom', 22, 'Male');
$arr = $obj->__toArray();
print_r($arr);
Copy after login

The output result is as follows:

Array
(
    [name] => Tom
    [age:protected] => 22
    [gender:MyClass:private] => Male
)
Copy after login

In this example, we define a __toArray() method in the MyClass class to add all attributes and attributes of the class The values ​​are returned in an array. Calling the $obj->__toArray() method will return an array containing all attributes and attribute values.

In addition to the above methods, we can also use PHP's own functions to convert PHP objects into arrays. The most commonly used functions are get_object_vars() and json_decode().

The get_object_vars() function can obtain all attributes of an object and return an associative array containing attribute names and attribute values.

The json_decode() function can convert a JSON format string into a PHP array. After converting the object into a JSON-formatted string, we can use the json_decode() function to convert the string into an array.

The following is a sample code that demonstrates how to use these two functions to convert a PHP object into an array:

class MyClass {
    public $name;
    protected $age;
    private $gender;

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

$obj = new MyClass('Tom', 22, 'Male');

// 将对象转化为数组
$arr1 = get_object_vars($obj);
$arr2 = json_decode(json_encode($obj), true);

print_r($arr1);
print_r($arr2);
Copy after login

The output is as follows:

Array
(
    [name] => Tom
    [age] => 22
    [gender] => Male
)
Array
(
    [name] => Tom
    [age] => 22
    [gender] => Male
)
Copy after login

In this example, We use the get_object_vars() function and json_decode() function to convert the PHP object into an array and print the result.

To sum up, we can use the Reflection API, magic method, get_object_vars() function and json_decode() function to convert PHP objects into arrays. Although these methods are different, the ultimate goal is to convert the object into an array so that we can process and operate it.

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