In PHP, statistical tasks that deal with two-dimensional arrays are common requirements, especially when facing complex data sets, how to extract and count specific values. array_column() and array_count_values() are two very useful functions that help developers efficiently extract data from two-dimensional arrays and perform statistical analysis.
This article will combine these two functions to demonstrate how to handle data statistics in two-dimensional arrays.
The array_column() function is used to return the value of a column in the array. When working with two-dimensional arrays, if we are only interested in the data of a certain column, we can use array_column() to extract all values of that column.
Suppose we have a two-dimensional array containing information about multiple users, including their names, emails, cities, etc.
<?php
$users = [
['name' => 'Alice', 'email' => 'alice@m66.net', 'city' => 'New York'],
['name' => 'Bob', 'email' => 'bob@m66.net', 'city' => 'Los Angeles'],
['name' => 'Charlie', 'email' => 'charlie@m66.net', 'city' => 'New York'],
['name' => 'David', 'email' => 'david@m66.net', 'city' => 'Chicago']
];
// Extract all city names
$cities = array_column($users, 'city');
print_r($cities);
?>
Output:
Array
(
[0] => New York
[1] => Los Angeles
[2] => New York
[3] => Chicago
)
In the above example, we use array_column() to extract the city information of each user in the two-dimensional array $users .
The array_count_values() function can count the number of times each value appears in an array. It is very useful for processing extracted data, especially when we need to perform frequency statistics on certain data.
Next we will count the distribution of user cities.
<?php
// use array_count_values() Come and count the number of occurrences in the city
$cityCounts = array_count_values($cities);
print_r($cityCounts);
?>
Output:
Array
(
[New York] => 2
[Los Angeles] => 1
[Chicago] => 1
)
With array_count_values() we get the number of times each city appears in the array.
Now, we combine these two functions to process data statistics tasks in two-dimensional arrays. For example, we want to count the city distribution of all users.
<?php
// Two-dimensional array data
$users = [
['name' => 'Alice', 'email' => 'alice@m66.net', 'city' => 'New York'],
['name' => 'Bob', 'email' => 'bob@m66.net', 'city' => 'Los Angeles'],
['name' => 'Charlie', 'email' => 'charlie@m66.net', 'city' => 'New York'],
['name' => 'David', 'email' => 'david@m66.net', 'city' => 'Chicago'],
['name' => 'Eve', 'email' => 'eve@m66.net', 'city' => 'New York']
];
// Extract city information
$cities = array_column($users, 'city');
// Statistics on the number of occurrences in cities
$cityCounts = array_count_values($cities);
// Output statistics
print_r($cityCounts);
?>
Output:
Array
(
[New York] => 3
[Los Angeles] => 1
[Chicago] => 1
)
In this example, we first use array_column() to extract the city information of all users, and then use array_count_values() to count the number of occurrences in each city. In this way, we can obtain statistical results efficiently.
By combining array_column() and array_count_values() , we can quickly extract data from a two-dimensional array and perform frequency statistics. This method is very useful in handling user data, log data and other scenarios.
In actual development, these built-in functions provided by PHP can greatly simplify our coding work and reduce the tedious process of manual loops and counting. By using these functions reasonably, we can write efficient and easy-to-maintain code.