Home >Backend Development >PHP Problem >How to convert object to array in PHP

How to convert object to array in PHP

PHPz
PHPzOriginal
2023-04-20 09:07:48702browse

In PHP development, it is often necessary to convert objects into arrays for operation or output. Object is an advanced data type, while array is a classic data structure in PHP, and conversion between the two is also a very common operation. This article will introduce how to convert objects into arrays in PHP.

1. Use type conversion

In PHP, you can convert an object into an array through forced type conversion. Specifically, you can use the following code to achieve this:

$obj = new stdClass(); // 创建一个对象
$arr = (array) $obj; // 将对象强制转换为数组
print_r($arr); // 打印数组

This method is very simple. You only need to enclose the object in parentheses and add the type identifier "array" to convert it into an array. However, this method can only convert an object's properties (including public and protected properties), not its methods.

2. Use the built-in method of the object

The object in PHP itself has a built-in method that can be converted into an array. This method converts all the properties and methods of the object (including public properties, protected properties, and private properties) into arrays. Specifically, you can use the following code to achieve:

$obj = new stdClass(); // 创建一个对象
$arr = get_object_vars($obj); // 将对象转换为本质的数组
print_r($arr); // 打印数组

This method is more flexible than cast because it can convert all properties and methods of the object. However, it should be noted that if the value of a property in the object is an object, this method will only convert the object type of the property to an array type.

3. Use the recursive method

If you need to convert all the properties and internal objects of the object into arrays, you can use the recursive method. The recursive method needs to iterate through all the properties and methods of the object in sequence and convert them all into arrays. Specifically, it can be implemented using the following code:

function objectToArray($obj) {
  if (is_object($obj)) $obj = get_object_vars($obj);
  if (is_array($obj)) {
    return array_map(__FUNCTION__, $obj); // 递归调用,直到转换完成
  }
  else {
    return $obj; // 返回结果数组
  }
}

$obj = new stdClass(); // 创建一个对象
$arr = objectToArray($obj); // 将对象转换为数组
print_r($arr); // 打印数组

The biggest advantage of this method is that it can recursively convert all the internal objects of the object into arrays, ensuring the integrity of the conversion results.

Summary

In PHP development, converting objects to arrays is a very common operation. This can be accomplished using casts, built-in methods of objects, or recursive methods. Different methods need to be selected according to the specific situation in order to get the best conversion results.

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