How to convert php object array to ordinary array
In PHP development, we often use the conversion between object arrays and ordinary arrays, especially when processing data, this conversion is very common. Therefore, this article will explain how to convert a PHP object array into a normal array.
In PHP, an object array refers to an array composed of objects, while a normal array refers to an array composed of basic data types. Object arrays are usually returned by database query results, especially when using an ORM framework. The ORM framework will encapsulate the data in the database into an object array.
The conversion between object array and ordinary array is very simple and can be completed by using the built-in functions in PHP. Below we will explain step by step how to convert.
- Built-in functions in PHP
In PHP, there are two built-in functions that can complete the conversion between object arrays and ordinary arrays:
- get_object_vars(): Convert the object to an array and return an associative array composed of attributes, where the key is the attribute name and the value is the attribute value.
- json_decode(): Convert a JSON-formatted string into a PHP instance or array, and return a standard PHP array.
- Using the get_object_vars() function
We can convert the object array into a normal array through the get_object_vars() function. This function takes an object parameter and returns an associative array of properties. The following is a sample code using the get_object_vars() function:
class Person { public $name = "John"; public $age = 25; private $password = "123456"; public function showDetails() { echo $this->name . " is " . $this->age . " years old \n"; } } $person1 = new Person(); $person2 = new Person(); $person2->name = "Jessica"; $person2->age = 30; $personArray1 = get_object_vars($person1); $personArray2 = get_object_vars($person2); print_r($personArray1); print_r($personArray2);
Output:
Array ( [name] => John [age] => 25 ) Array ( [name] => Jessica [age] => 30 )
As you can see, after using the get_object_vars() function, we successfully converted the object array into a normal array.
- Use the json_decode() function
In addition to using the get_object_vars() function, we can also use the json_decode() function to convert the object array into a normal array. This function accepts a JSON string parameter and returns a standard PHP array. The following is a sample code using the json_decode() function:
class Person { public $name = "John"; public $age = 25; private $password = "123456"; public function showDetails() { echo $this->name . " is " . $this->age . " years old \n"; } } $person1 = new Person(); $person2 = new Person(); $person2->name = "Jessica"; $person2->age = 30; $json1 = json_encode($person1); $json2 = json_encode($person2); $personArray1 = json_decode($json1, true); $personArray2 = json_decode($json2, true); print_r($personArray1); print_r($personArray2);
Output:
Array ( [name] => John [age] => 25 ) Array ( [name] => Jessica [age] => 30 )
After using the json_decode() function, we also successfully converted the object array into a normal array.
- Summary
Converting a PHP object array to a normal array is very simple. You only need to use the get_object_vars() function or the json_decode() function to complete. Using the get_object_vars() function can directly convert the object into an array, while using the json_decode() function requires converting the object to a string in JSON format first, and then converting the string into an array. No matter which method is used, it can meet our needs when processing data.
The above is the detailed content of How to convert php object array to ordinary array. 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









