Home  >  Article  >  Backend Development  >  How to use an array of objects in an array in php

How to use an array of objects in an array in php

PHPz
PHPzOriginal
2023-04-20 15:01:49690browse

So far, PHP is one of the most popular programming languages ​​in the world. It's easy to use while handling complex tasks. It is very common for programmers to use arrays. Arrays in PHP can store not only basic data types (such as strings, integers, floating point numbers) but also more complex data types, such as object arrays.

In this article, we will introduce how to use arrays of objects in arrays.

What is an object array?

In PHP, an object array refers to an array that stores objects. An object is a data type with properties and methods that can be created using a class or structure, while an array is a data type that can store multiple values.

When each element in the object array is an object, it can be called an object array.

Create an object array

Creating an object array in PHP is very simple. First, you need to define a class to create your object. Here is a simple class definition.

class Person {
  public $name;
  public $age;

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

Now, we can create an array of objects using the Person class defined above. In the following example, we create an array of Person objects and then output the properties of each object in the array.

$people = array(
    new Person('Tom', 20),
    new Person('Jerry', 25),
    new Person('Mary', 30)
);

foreach ($people as $person) {
  echo $person->name . " is " . $person->age . " years old.\n";
}

The output will be:

Tom is 20 years old.
Jerry is 25 years old.
Mary is 30 years old.

As you can see, using an array of objects within an array makes the code more flexible. You can use object arrays to store all types of custom objects and easily access their properties and methods.

Getting a specific object from an array of objects

In some cases, you may need to get a specific object from an array of objects. To do this, you can use PHP's built-in array_column function.

The array_column function is used to get a specific column in the array. Since objects use properties to store data, you can use this function to find specific properties for an array of objects.

For example, the following code will find an object named "Jerry" in the $people array.

$jerry = array_column($people, null, 'name')['Jerry'];
echo $jerry->name . " is " . $jerry->age . " years old.";

The output will be:

Jerry is 25 years old.

Add an object to the object array

If you need to add a new object to the existing object array, you can use PHP's built-in array function array_push. In the following example, we will add a new Person object to the $people array.

array_push($people, new Person('Adam', 35));

foreach ($people as $person) {
  echo $person->name . " is " . $person->age . " years old.\n";
}

The output will be:

Tom is 20 years old.
Jerry is 25 years old.
Mary is 30 years old.
Adam is 35 years old.

Deleting an object in an object array

When you no longer need a specific object in an object array, you can use PHP's built-in functions unset deletes it.

For example, the following code will delete the object of the person named "Jerry" from the $people array.

unset($people[1]);

foreach ($people as $person) {
  echo $person->name . " is " . $person->age . " years old.\n";
}

The output will be:

Tom is 20 years old.
Mary is 30 years old.
Adam is 35 years old.

Conclusion

In this article, we introduced how to use arrays of objects. We learned how to create an array of objects, how to get a specific object from the array of objects, how to add objects to the array of objects, and how to delete objects from the array of objects. Using PHP's object arrays, you can easily store and access custom objects and complete complex programming tasks more easily.

The above is the detailed content of How to use an array of objects in an 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