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

How to convert object to array object array in php

PHPz
PHPzOriginal
2023-04-25 10:32:44481browse

PHP is an object-oriented programming language where objects are the basic building blocks in code. Array is another commonly used data type that consists of elements that can be accessed by index. In some cases, converting an object to an array or an array of objects can make the code more convenient and concise. This article will introduce how to achieve this function in PHP.

  1. Convert Object to Array

In PHP, you can use cast to convert an object to an array. The following is an example:

class Person {
  public $name;
  public $age;

  public function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
}

$person = new Person("Tom", 20);

$array = (array) $person;

print_r($array);

The output is:

Array
(
    [name] => Tom
    [age] => 20
)

In this example, we first define a Person class, which has two attributes: name and age. Then, we create an instance $person. Next, use (array) syntax to convert $person to an array. Finally, use the print_r() function to output the values ​​of the array.

It should be noted that when using forced type conversion to convert an object into an array, if the object has private properties or protected properties, the values ​​of these properties will not be included in the array.

  1. Convert an object array to an array

In PHP, you can use a callback function to convert an object array to an array. The following is an example:

class Person {
  public $name;
  public $age;

  public function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
}

$persons = array(
  new Person("Tom", 20),
  new Person("Jerry", 22),
  new Person("Alice", 18)
);

$array = array_map(function($person) {
  return (array) $person;
}, $persons);

print_r($array);

The output is:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 20
        )

    [1] => Array
        (
            [name] => Jerry
            [age] => 22
        )

    [2] => Array
        (
            [name] => Alice
            [age] => 18
        )
)

In this example, we first define a Person class, which has two attributes: name and age. Then, we create an object array $persons that contains three Person instances. Next, we use the array_map() function to convert each Person instance to an array, resulting in a new array.

It is important to note that since each Person instance will be converted to an array, each element in each object array will also be an array.

Conclusion

In PHP, converting an object to an array or an array of objects is a common operation and can make the code more convenient and concise. We can use casts or callback functions to implement these conversion operations. Note that objects with private or protected properties may not be fully converted to arrays.

The above is the detailed content of How to convert object to array object 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