In PHP, array_chunk and array_column are two very useful functions that can help us perform flexible operations on arrays. By using these two functions reasonably, we can easily implement the operation of splitting two-dimensional arrays by columns. This article will introduce in detail how to combine array_chunk and array_column to achieve this goal.
array_column : This function can extract the value of a column from a multidimensional array. Its common usage is to get all values of a specified column without manually looping.
array_column(array $array, mixed $column_key, mixed $index_key = null)
array_chunk : This function divides an array into multiple small arrays, each small array containing up to a specified number of elements. This allows for efficient splitting of large arrays.
array_chunk(array $array, int $size, bool $preserve_keys = false)
Suppose we have a two-dimensional array where each element contains data for several columns. If we want to extract a column and split the data of the column into multiple small arrays according to a certain size, we can use a combination of array_column and array_chunk .
Suppose we have the following two-dimensional array:
$data = [
['id' => 1, 'name' => 'Tom', 'age' => 28],
['id' => 2, 'name' => 'Jerry', 'age' => 30],
['id' => 3, 'name' => 'Mickey', 'age' => 25],
['id' => 4, 'name' => 'Donald', 'age' => 35],
['id' => 5, 'name' => 'Goofy', 'age' => 40],
['id' => 6, 'name' => 'Pluto', 'age' => 22]
];
If we only want to extract the name column and split it every 2 elements, we can use the following code:
// extract 'name' List
$names = array_column($data, 'name');
// Press each 2 Split elements
$chunkedNames = array_chunk($names, 2);
// Output split array
print_r($chunkedNames);
Output result:
Array
(
[0] => Array
(
[0] => Tom
[1] => Jerry
)
[1] => Array
(
[0] => Mickey
[1] => Donald
)
[2] => Array
(
[0] => Goofy
[1] => Pluto
)
)
In practical applications, we often need to split more complex arrays. Suppose we have a list of URL addresses and want to extract the main domain name part of each URL and then split it by every 3 domain names. Assume the original data is as follows:
$data = [
['url' => 'http://www.m66.net/page1', 'other' => 'data1'],
['url' => 'http://www.m66.net/page2', 'other' => 'data2'],
['url' => 'http://www.m66.net/page3', 'other' => 'data3'],
['url' => 'http://www.m66.net/page4', 'other' => 'data4'],
['url' => 'http://www.m66.net/page5', 'other' => 'data5'],
['url' => 'http://www.m66.net/page6', 'other' => 'data6']
];
We first use array_column to extract all URLs, then use parse_url function to obtain the main domain name, and finally use array_chunk to split the domain name into three groups.
// extract 'url' List
$urls = array_column($data, 'url');
// Get each URL The main domain name part
$domains = array_map(function($url) {
return parse_url($url, PHP_URL_HOST);
}, $urls);
// Press each 3 Domain name split
$chunkedDomains = array_chunk($domains, 3);
// Output split domain name array
print_r($chunkedDomains);
Output result: