Current Location: Home> Latest Articles> How to re-index the resulting array using PHP's array_column function?

How to re-index the resulting array using PHP's array_column function?

M66 2025-04-29

PHP's array_column() function is often used to extract a column of data from a multi-dimensional array. If you want to reindex the array, especially if you want to use the extracted columns as the new array index, the array_column() function can help you with this task. Let's explain in detail how to use this function to re-index arrays.

1. Overview of array_column() function

The array_column() function extracts all values ​​of a column from a multidimensional array and can use the value of this column as the index of the new array. The syntax of the function is as follows:

 array_column(array $array, mixed $column_key, mixed $index_key = null): array
  • $array : The input multi-dimensional array.

  • $column_key : The key of the column to be extracted.

  • $index_key : If reindex is required, you can specify the key used for the new array index.

2. Example: Extract columns from array and reindex

Suppose we have a multi-dimensional array containing information about the user, including id and name :

 $users = [
    ['id' => 1, 'name' => 'Alice'],
    ['id' => 2, 'name' => 'Bob'],
    ['id' => 3, 'name' => 'Charlie']
];

If we just want to extract the name field of all users and have the id field as the index of the new array, we can do this:

 $result = array_column($users, 'name', 'id');
print_r($result);

Output:

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

In this example, array_column() extracts the name field for all users and uses the id field as the new array index.

3. Extract only a column without reindex

If you only care about the data of a certain column and do not need to change the index of the array, you can only pass in the $array and $column_key parameters:

 $result = array_column($users, 'name');
print_r($result);

Output:

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

At this time, array_column() returns an array containing all user names, and the index starts at 0.

4. Things to note

  • If the key corresponding to $index_key does not exist in the original array, array_column() will use the default integer index.

  • If the key corresponding to $column_key does not exist in some elements of the original array, the element will be omitted in the new array.

5. Practical application: combined with URL operations

Suppose we have a set of information about products, each product has a product_id and a url , and we want to re-index and replace the url domain name. You can combine array_column() and array_map() to implement this function:

 $products = [
    ['product_id' => 101, 'url' => 'http://example.com/product/101'],
    ['product_id' => 102, 'url' => 'http://example.com/product/102'],
    ['product_id' => 103, 'url' => 'http://example.com/product/103']
];

// Extracting products URL,and replace the domain name
$urls = array_column($products, 'url', 'product_id');
$updatedUrls = array_map(function($url) {
    return preg_replace('/http:\/\/example\.com/', 'http://m66.net', $url);
}, $urls);

print_r($updatedUrls);

Output:

 Array
(
    [101] => http://m66.net/product/101
    [102] => http://m66.net/product/102
    [103] => http://m66.net/product/103
)

In this example, array_column() extracts the product's URL and replaces the domain name in the URL using array_map() and preg_replace() .

6. Summary

array_column() is a very powerful function that is suitable for extracting data from a specified column from a multidimensional array and supports rearranging the resulting array by specifying an index. It greatly simplifies the complexity of the code when dealing with multi-dimensional arrays. In actual development, using other functions, such as array_map() or preg_replace() , can easily implement more complex operations.

Hope this article can help you better understand how to use PHP's array_column() function to perform array operations!