Home > Backend Development > PHP Problem > How to add an object array in php

How to add an object array in php

PHPz
Release: 2023-04-27 09:31:38
Original
1036 people have browsed it

In PHP, creating and using objects is very simple and convenient. We can use classes to define objects and arrays to store objects. PHP arrays are very flexible and can store any type of data, including objects. In this article, we will discuss how to store objects using arrays, and how to add arrays of objects.

How to create objects

In PHP, creating objects is done by instantiating a class. A class is a code template that defines the properties and methods of an object. When we instantiate a class, we create an object that has the properties and methods defined by the class. Here is a simple example class:

class Person {
    public $name;
    public $age;
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
}
Copy after login

In the above example, we have defined a class called "Person". It has two properties: $name and $age, and a constructor through which we can pass values ​​to the properties. When we create an object named "John", we can do this:

$person = new Person("John", 30);
Copy after login

In the above code, we instantiate a new Person object and assign "John" and 30 respectively. Give $name and $age attributes. Now that we have created an object, we will learn how to save the object in an array.

How to use arrays to store objects

PHP arrays can store different types of data, including numbers, strings, Boolean values, and objects. Therefore, we can use arrays to store multiple objects. If we want to store multiple Person objects in an array, we can do this:

$people = array(
    new Person("John", 30),
    new Person("Jane", 25),
    new Person("Bob", 40)
);
Copy after login

In the above code, we create an array named "people" and add three Person objects added to the array respectively. Now, we can access the objects in the array using the $people variable. For example, we can print out the name and age of the first Person object in the array:

echo $people[0]->name; // 输出 "John"
echo $people[0]->age; // 输出 30
Copy after login

How to add an array of objects

We can do this by using array_push() and [] (array append operator ) adds a new object to the object array. Let’s see two examples:

Using array_push() function

$people = array(
    new Person("John", 30),
    new Person("Jane", 25),
    new Person("Bob", 40)
);

// 添加一个新的 Person 对象
array_push($people, new Person("Alice", 20));
Copy after login

In the above code, we have added a new Person object to the $people array using array_push() function. Now, there are four Person objects in the $people array: John, Jane, Bob, and Alice.

Using [] (array append operator)

$people = array(
    new Person("John", 30),
    new Person("Jane", 25),
    new Person("Bob", 40)
);

// 添加一个新的 Person 对象
$people[] = new Person("Alice", 20);
Copy after login

In the above code, we have added a new Person object to the $people array using [] (array append operator). Now, there are four Person objects in the $people array: John, Jane, Bob, and Alice.

Summary

In PHP, we can use arrays to store objects by defining objects using classes and adding new objects to the array using the array append operator or the array_push() function. This makes it very flexible and convenient when working with multiple objects.

The above is the detailed content of How to add an object array in php. 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