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

How to convert object to array in PHP

Apr 20, 2023 am 09:07 AM

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); // 打印数组
Copy after login

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); // 打印数组
Copy after login

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); // 打印数组
Copy after login

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

How to Implement message queues (RabbitMQ, Redis) in PHP?

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

Does PHP array deduplication need to be considered for performance losses?

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles