How to convert object array into array in php
PHP is a very popular programming language that supports object-oriented programming (OOP) and array operations. In PHP, we sometimes need to convert an array of objects into a normal array. This article will introduce how to convert an array of objects into an array in PHP.
- Use the json_decode function
There is a very convenient function in PHP that can convert a JSON format string into a PHP array: json_decode.
Object arrays can be encoded into JSON format strings. Therefore, we can first encode the object array into a JSON string and then use the json_decode function to convert it into a PHP array. Here is an example:
<?php class Person { public $name; public $age; public $gender; } // 创建对象数组 $persons = []; $person1 = new Person(); $person1->name = "张三"; $person1->age = 20; $person1->gender = "男"; $persons[] = $person1; $person2 = new Person(); $person2->name = "李四"; $person2->age = 22; $person2->gender = "男"; $persons[] = $person2; // 将对象数组编码成JSON字符串 $json_str = json_encode($persons); // 将JSON字符串解码成PHP数组 $person_arr = json_decode($json_str, true); print_r($person_arr); ?>
This will output the following results:
Array ( [0] => Array ( [name] => 张三 [age] => 20 [gender] => 男 ) [1] => Array ( [name] => 李四 [age] => 22 [gender] => 男 ) )
- Using the Serializable interface
Another way to convert an array of objects into an array The method is to implement the Serializable interface. This method requires a custom method in the class to convert the object array of this class into a serializable string. Here is an example:
<?php class Person implements Serializable { public $name; public $age; public $gender; // 实现Serializable接口的方法 public function serialize() { return serialize([$this->name, $this->age, $this->gender]); } // 实现Serializable接口的方法 public function unserialize($serialized) { [$this->name, $this->age, $this->gender] = unserialize($serialized); } } // 创建对象数组 $persons = []; $person1 = new Person(); $person1->name = "张三"; $person1->age = 20; $person1->gender = "男"; $persons[] = $person1; $person2 = new Person(); $person2->name = "李四"; $person2->age = 22; $person2->gender = "男"; $persons[] = $person2; // 将对象数组编码成可序列化的字符串 $serialized_str = serialize($persons); // 将序列化的字符串转换为PHP数组 $person_arr = unserialize($serialized_str); print_r($person_arr); ?>
This will output the following results:
Array ( [0] => Person Object ( [name] => 张三 [age] => 20 [gender] => 男 ) [1] => Person Object ( [name] => 李四 [age] => 22 [gender] => 男 ) )
Summary
This article introduces two methods of converting an array of objects into an array: using the json_decode function and Implement the Serializable interface. Developers can choose different implementation methods according to their own needs. Remember that when using the second method, the object class needs to implement the Serializable interface.
The above is the detailed content of How to convert object array into array in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
