Home  >  Article  >  Backend Development  >  What is the function in php to convert an object into an array?

What is the function in php to convert an object into an array?

青灯夜游
青灯夜游Original
2022-01-21 18:46:122171browse

The function in php that converts objects into arrays is "get_object_vars()". This function is used to obtain the attributes of a given object and return an associative array composed of object attributes. The syntax is "get_object_vars($ object)".

What is the function in php to convert an object into an array?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Convert objects to arrays in php The function is "get_object_vars()".

get_object_vars() function is used to get the attributes of a given object and returns an associative array composed of object attributes. But if the object has no attributes, NULL is returned.

Syntax:

get_object_vars($object)

Returns an associative array composed of properties defined in the object specified by obj.

Note:

In versions prior to PHP 4.2.0, if variables declared in the obj object instance are not assigned a value, they will not be in the returned array . After PHP 4.2.0, these variables will be assigned null values ​​as key names.

Example:

<?php
 
class gfg {
    private $geeks = 0.02;
    public $for = 1;
    public $Geeks = "php";
    private $GEEKS;
    static $e;
     
    public function example() {
        var_dump(get_object_vars($this));
    }
}
 

$example = new gfg;
 
var_dump(get_object_vars($example));
  
$example->example();
  
?>

What is the function in php to convert an object into an array?

Recommended learning: "PHP Video Tutorial"

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