Home  >  Article  >  Backend Development  >  How to convert object array into array in php

How to convert object array into array in php

PHPz
PHPzOriginal
2023-04-20 10:10:48446browse

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.

  1. 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] => 男
        )

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

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