Home  >  Article  >  Backend Development  >  How to convert object to array in php

How to convert object to array in php

PHPz
PHPzOriginal
2023-04-26 14:26:062004browse

PHP is a very useful programming language with powerful object-oriented features. In PHP, we can easily convert objects into arrays. This article will introduce various methods of converting objects to arrays in PHP.

1. Use the get_object_vars() function

get_object_vars() is a PHP built-in function, used to get all the attributes and their values ​​of the object, and return an array. The syntax format of this function is as follows:

array get_object_vars ( object $object )

where $object represents the object to be converted.

The following sample code demonstrates how to convert an object to an array:

class MyClass {
   public $name = '张三';
   protected $age = 18;
   private $sex = '男';
}

$myObj = new MyClass();

$arr = get_object_vars($myObj);

print_r($arr);

Here an object $myObj is created through the class MyClass and converted to an array$ using the get_object_vars() function arr, and finally output the array on the console.

The results are as follows:

Array
(
    [name] => 张三
)

As you can see, due to the different access permissions of the $age and $sex attributes, only $name is converted to an array.

2. Use type casting (type conversion)

In addition to using built-in functions, we can also use the type conversion syntax in PHP to convert objects into arrays. Type conversion syntax involves prepending the variable with the name of the data type to be converted, such as (array), (string), (int), etc.

In the following sample code, we use (array) to convert the object to an array:

class MyClass {
   public $name = '张三';
   protected $age = 18;
   private $sex = '男';
}

$myObj = new MyClass();
$arr = (array) $myObj;
print_r($arr);

The difference here from the previous example is that when converting the object $myObj to an array, we The (array) type conversion syntax is used.

Run the above code and you will get the same output as the array shown before.

3. Use the json_decode() function

In addition to using get_object_vars() and type casting, we can also use the json_decode() function to convert objects into arrays or associative arrays.

json_decode() function can convert JSON string to PHP array or associative array, so if we serialize the object to JSON string and then deserialize it using json_decode() function, we can Convert an object to an array or associative array.

The following sample code demonstrates this process:

class MyClass {
   public $name = '张三';
   protected $age = 18;
   private $sex = '男';
}

$myObj = new MyClass();

$jsonString = json_encode($myObj);

$arr = json_decode($jsonString, true);

print_r($arr);

Here we create an object $myObj through the class MyClass and use the json_encode() function to serialize it into a JSON string $ jsonString. Next, we use the json_decode() function to decode the string into an associative array $arr.

In the json_decode() function, the second parameter is set to true, which means that after decoding, an associative array is returned instead of an object. If this parameter is not set, an object is returned instead of an array.

Run the code and you will get the same output as the previous example.

Summary

In PHP, we can convert objects into arrays in many ways. The get_object_vars() function can conveniently convert all the properties of an object and their values ​​into arrays, while the type casting and json_decode() functions can serialize the object into a string and then convert it into an array through the conversion function. No matter which method is used, as long as you choose the method suitable for the current scenario, you can easily convert the object into an array to better meet business needs.

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