Current Location: Home> Latest Articles> How to use array_column to extract a column in a multidimensional array

How to use array_column to extract a column in a multidimensional array

M66 2025-04-28

How to extract a column in a multidimensional array through array_column and dynamically set the second parameter of array_chunk according to the array length

In PHP, array_column and array_chunk are very commonly used array operation functions. array_column can extract the value of a column in a two-dimensional array, while array_chunk can divide the array into multiple small pieces. If we need to dynamically set the second parameter of array_chunk according to the length of the array, combined with the use of array_column , it can help us process array data more flexibly.

1. What are array_column and array_chunk ?

  • array_column($array, $column_key, $index_key) : Extract the value of the specified column from a two-dimensional array. It receives three parameters:

    • $array : original array.

    • $column_key : The column name or column index that needs to be extracted.

    • $index_key : optional, representing the index column. If not specified, a tiled array is returned.

  • array_chunk($array, $size, $preserve_keys = false) : divide an array into multiple small arrays, each small array contains $size elements. Returns a multi-dimensional array.

    • $array : original array.

    • $size : The size of each subarray.

    • $preserve_keys : Whether to preserve the keys of the original array.

2. Use array_column to extract a column in a multi-dimensional array

Suppose we have a multidimensional array with each element representing a person's information (such as name, age, gender, etc.). We can use array_column to extract one of the columns of data. for example:

 $people = [
    ['name' => 'Alice', 'age' => 25, 'gender' => 'female'],
    ['name' => 'Bob', 'age' => 30, 'gender' => 'male'],
    ['name' => 'Charlie', 'age' => 35, 'gender' => 'male'],
    ['name' => 'David', 'age' => 28, 'gender' => 'male']
];

// extract 'name' List
$names = array_column($people, 'name');
print_r($names);

Output:

 Array
(
    [0] => Alice
    [1] => Bob
    [2] => Charlie
    [3] => David
)

As shown above, array_column extracts the name column in the people array.

3. Dynamically set the second parameter of array_chunk

Now suppose we want to split the array into multiple subarrays, and the size of the subarray is dynamically set according to the length of the original array. For example, if the length of the array is larger, the size of each subarray can be set to 10; if the length of the array is smaller, the size of each subarray can be set to 5. We can use the count() function to get the length of the array and determine the second parameter of array_chunk based on this length.

 $names = array_column($people, 'name');
$array_length = count($names);

// Dynamically set the size of each subarray
$chunk_size = ($array_length > 3) ? 2 : 1;  // If the array length is greater than3,Then each subarray contains2Elements,Otherwise, include1Elements

$chunked = array_chunk($names, $chunk_size);
print_r($chunked);

Output (assuming the array length is 4):

 Array
(
    [0] => Array
        (
            [0] => Alice
            [1] => Bob
        )

    [1] => Array
        (
            [0] => Charlie
            [1] => David
        )
)

As shown above, we dynamically set the size of each subarray based on the length of the names array extracted by array_column .

4. URL replacement example

If in actual development, you need to process data with URLs and want to replace the domain names of these URLs with m66.net , you can use str_replace to implement it:

 $urls = [
    'https://www.example.com/page1',
    'https://www.example.com/page2',
    'https://www.example.com/page3',
];

// use str_replace Replace domain name
$updated_urls = array_map(function($url) {
    return preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $url);
}, $urls);

print_r($updated_urls);

Output: