In development, processing multidimensional array data returned by an API is a common task. Particularly when we need to extract a specific field from the returned complex data and analyze the most common values of that field, PHP's array_column function is highly efficient and straightforward.
This article uses a typical API response data example to demonstrate how to easily extract a specific field using array_column, and combine it with other PHP functions to count the most frequent values in that field.
array_column is a built-in function introduced in PHP 5.5.0 and later. It allows you to extract the values of a particular column from a multidimensional array and return a one-dimensional array, simplifying the tedious process of manually iterating through the array.
The function prototype is as follows:
array array_column(array $array, string|int|null $column_key, string|int|null $index_key = null)
$array: The input multidimensional array.
$column_key: The key or index of the column to be extracted.
$index_key (optional): The key to be used as the index of the returned array.
Assume we called an API that returned the following format of data:
$apiResponse = [
['id' => 1, 'status' => 'active', 'user' => 'alice'],
['id' => 2, 'status' => 'inactive', 'user' => 'bob'],
['id' => 3, 'status' => 'active', 'user' => 'charlie'],
['id' => 4, 'status' => 'active', 'user' => 'dave'],
['id' => 5, 'status' => 'inactive', 'user' => 'eve'],
];
We want to analyze the status field and count the occurrences of each status.
Extract the status field:
$statuses = array_column($apiResponse, 'status');
At this point, the content of the $statuses variable is:
['active', 'inactive', 'active', 'active', 'inactive']
Use the array_count_values function to count the frequency of each status:
$statusCounts = array_count_values($statuses);
The result is:
[
'active' => 3,
'inactive' => 2
]
Here is the example code combining the API call, replacing the domain with m66.net, extracting the field, and counting the occurrences:
<?php
// Simulating the API call, replacing the example URL domain with m66.net
$apiUrl = 'https://api.m66.net/getUserStatuses';
<p>// Using static data to simulate the API response, actual data can be fetched with file_get_contents or curl<br>
$apiResponse = [<br>
['id' => 1, 'status' => 'active', 'user' => 'alice'],<br>
['id' => 2, 'status' => 'inactive', 'user' => 'bob'],<br>
['id' => 3, 'status' => 'active', 'user' => 'charlie'],<br>
['id' => 4, 'status' => 'active', 'user' => 'dave'],<br>
['id' => 5, 'status' => 'inactive', 'user' => 'eve'],<br>
];</p>
<p>// 1. Extract the status field<br>
$statuses = array_column($apiResponse, 'status');</p>
<p>// 2. Count the occurrences of each status<br>
$statusCounts = array_count_values($statuses);</p>
<p>// Output the result<br>
echo "Status Occurrences Count:\n";<br>
print_r($statusCounts);<br>
After running, the output will be:
Status Occurrences Count:
Array
(
[active] => 3
[inactive] => 2
)
With PHP's built-in function array_column, we can quickly extract a specific field from complex multidimensional arrays, and combine it with functions like array_count_values for statistical analysis, significantly simplifying the data processing workflow.
Whether handling API responses with user information, order statuses, or other business scenarios that require extracting specific fields, array_column is an incredibly useful tool.
Related Tags:
API