PHP advanced features: in-depth exploration of arrays and collections

PHPz
Release: 2024-06-04 12:55:57
Original
236 people have browsed it

PHP provides multi-dimensional arrays and associative arrays, and provides a series of array functions. Collection classes provide an easy-to-use API to implement advanced collection operations, including creating, filtering, mapping, and aggregating collections. You can use these features to build tree structures, filter and map data, and perform aggregation operations.

PHP advanced features: in-depth exploration of arrays and collections

PHP Advanced Features: In-depth Exploration of Arrays and Collections

PHP provides a series of powerful and flexible array and collection operations , allowing developers to effectively manage and process complex data structures. This article will delve into the advanced features of PHP arrays and collections and provide real-world practical examples to help you take full advantage of these features.

1. Array features

  1. Multi-dimensional array

PHP supports multi-dimensional arrays, allowing the creation of embedded set of data structures. You can use array_walk_recursive() to recursively traverse a multi-dimensional array, or use array_reduce() to perform a reduction operation.

$tree = array(
    'level1' => array(
        'level2' => array(
            'data' => 'value'
        )
    )
);

array_walk_recursive($tree, function($value, $key) {
    echo "$key: $value\n";
});
Copy after login
  1. Associative array

Associative array uses key names to associate with values, making it easy to quickly find and retrieve values ​​based on key names. PHP provides comparison operators such as <=>, ==, and ==== for comparing values ​​in associative arrays.

$assoc = array(
    'name' => 'John Doe',
    'age' => 30
);

if ($assoc['name'] == 'John Doe') {
    echo 'Name matches';
}
Copy after login
  1. Array function

PHP provides a series of built-in array functions for manipulating arrays, such as array_merge() (merging arrays), array_filter() (filtering arrays), array_map() (mapping arrays), etc.

$numbers = array(1, 2, 3, 4, 5);

$filteredNumbers = array_filter($numbers, function($num) {
    return $num % 2 == 0;
});
Copy after login

2. Collection class

In addition to native arrays, PHP also introduces the Collection class, which provides a set of easy-to-use APIs to implement Advanced collection operations.

  1. Create a collection

You can use the Collection::make() method to create a Collection object .

$collection = Collection::make(['foo', 'bar', 'baz']);
Copy after login
  1. Filtering and mapping collections

Collection class provides filter() and map() method, used to filter and map elements in the collection respectively.

$filteredCollection = $collection->filter(function($item) {
    return is_string($item);
});

$mappedCollection = $collection->map(function($item) {
    return strtoupper($item);
});
Copy after login
  1. Aggregation collection

Collection class also provides sum(), ## Aggregation methods such as #average() and reduce() are used to calculate summary values ​​from a collection.

$sum = $collection->sum();
$average = $collection->average();
Copy after login

Practical case

Using multi-dimensional arrays to build a tree structure

$tree = array();
$tree['root'] = array(
    'left' => array(
        'data' => 'left branch'
    ),
    'right' => array(
        'data' => 'right branch'
    )
);

array_walk_recursive($tree, function($value, $key) {
    if ($value != 'root') {
        echo "$key: $value\n";
    }
});
Copy after login

Using collections to filter and map data

$users = [
    ['name' => 'John Doe', 'age' => 30],
    ['name' => 'Jane Doe', 'age' => 25],
];

$collection = Collection::make($users);

$filteredCollection = $collection->filter(function($user) {
    return $user['age'] > 25;
});

$mappedCollection = $collection->map(function($user) {
    return $user['name'];
});
Copy after login

The above is the detailed content of PHP advanced features: in-depth exploration of arrays and collections. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!