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

How to convert object to array object array in php

Apr 25, 2023 am 10:32 AM

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

The output is:

Array
(
    [name] => Tom
    [age] => 20
)
Copy after login

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

The output is:

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

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

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

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 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 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)