Home >Backend Development >PHP Tutorial >PHP advanced features: in-depth exploration of arrays and collections
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 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
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"; });
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'; }
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; });
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.
You can use the Collection::make()
method to create a Collection
object .
$collection = Collection::make(['foo', 'bar', 'baz']);
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); });
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();
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"; } });
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']; });
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!