How to convert php object array to ordinary array

PHPz
Release: 2023-04-23 09:48:39
Original
567 people have browsed it

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template