Home  >  Article  >  Backend Development  >  How to use objects in arrays in php

How to use objects in arrays in php

WBOY
WBOYOriginal
2023-05-11 09:58:36834browse

When programming in PHP, two data structures, arrays and objects, are often used. In actual development, sometimes you encounter situations where objects need to be stored in arrays. This article will explore how to use objects in arrays in PHP.

1. Create objects and arrays

Creating objects in PHP is very simple, just use the keyword new, followed by the class name. For example:

class Person {
    public $name;
    public $age;
}

$person = new Person();
$person->name = '小明';
$person->age = 18;

Creating an array is also very simple, just use square brackets []. For example:

$array = ['a', 'b', 'c'];

2. Add an object to an array

If you need to add an object to an array, you only need to assign the object directly to an element of the array. For example:

$person1 = new Person();
$person1->name = '小明';
$person1->age = 18;

$person2 = new Person();
$person2->name = '小红';
$person2->age = 20;

$array = [$person1, $person2];

At this time, $array contains two objects $person1 and $person2.

3. Accessing the objects in the array

Accessing the objects in the array is also very simple. You only need to use the array subscript and the object property name. For example:

echo $array[0]->name;    // 输出:小明
echo $array[0]->age;     // 输出:18
echo $array[1]->name;    // 输出:小红
echo $array[1]->age;     // 输出:20

4. Operate objects in arrays in loops

In development, it is often necessary to use loops to operate objects in arrays. For example:

foreach ($array as $person) {
    echo $person->name . '今年' . $person->age . '岁。';
}

Here we use the foreach loop to traverse all the elements in the array $array, access the attributes in each element, and output the information of each element.

5. Sort the objects in the array

In actual development, sometimes it is necessary to sort the objects stored in the array. For example:

class Person {
    public $name;
    public $age;
}

$person1 = new Person();
$person1->name = '小明';
$person1->age = 18;

$person2 = new Person();
$person2->name = '小红';
$person2->age = 20;

$person3 = new Person();
$person3->name = '小李';
$person3->age = 16;

$array = [$person1, $person2, $person3];

Now, we need to sort the elements in $array according to the $age property in $array. This can be achieved using the usort() function. For example:

usort($array, function($a, $b) {
    return $a->age - $b->age;
});

foreach ($array as $person) {
    echo $person->name . '今年' . $person->age . '岁。';
}

In the above code, we use the usort() function to sort the elements in the $array array, using an anonymous function to define the sorting rules. This anonymous function receives two parameters $a and $b, representing two array elements, and returns an integer indicating the size order of $a and $b.

During output, we used a foreach loop to traverse the elements in the array and output the information of each element.

6. Sorting objects in multi-dimensional arrays

For sorting objects in multi-dimensional arrays, you can use the usort() function in combination with the callback function. For example:

class Person {
    public $name;
    public $age;
}

$person1 = new Person();
$person1->name = '小明';
$person1->age = 18;

$person2 = new Person();
$person2->name = '小红';
$person2->age = 20;

$person3 = new Person();
$person3->name = '小李';
$person3->age = 16;

$array = [
    ['name' => '数组1', 'person' => $person1],
    ['name' => '数组2', 'person' => $person2],
    ['name' => '数组3', 'person' => $person3],
];

usort($array, function($a, $b) {
    return $a['person']->age - $b['person']->age;
});

foreach ($array as $item) {
    echo $item['name'] . '中的' . $item['person']->name . '今年' . $item['person']->age . '岁。';
}

In the above code, we create a multi-dimensional array $array, each element of the array is an associative array, which contains an object named person. Next, we use the usort() function and a callback function to sort the elements in the $array array according to the $person->age attribute. Finally, we use a foreach loop to output the sorted $array array.

Summary

This article introduces how to use objects in arrays in PHP, and discusses how to add objects to arrays, access objects in arrays, and operate objects in arrays in loops. Objects and sorting objects in arrays. In practical applications, it is necessary to choose the appropriate method to process data according to the specific situation in order to achieve better results.

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