Home Backend Development PHP Problem How to convert php object array to ordinary array

How to convert php object array to ordinary array

Apr 23, 2023 am 09:12 AM

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.

  1. 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.
  1. 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);
Copy after login

Output:

Array
(
    [name] => John
    [age] => 25
)

Array
(
    [name] => Jessica
    [age] => 30
)
Copy after login
Copy after login

As you can see, after using the get_object_vars() function, we successfully converted the object array into a normal array.

  1. 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);
Copy after login

Output:

Array
(
    [name] => John
    [age] => 25
)

Array
(
    [name] => Jessica
    [age] => 30
)
Copy after login
Copy after login

After using the json_decode() function, we also successfully converted the object array into a normal array.

  1. 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!

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

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1254
24