Current Location: Home> Latest Articles> How to quickly generate tag lists with array_column

How to quickly generate tag lists with array_column

M66 2025-05-11

In PHP, we often process array data, especially when processing a large amount of information obtained from a database or other data source. The array_column function is a very practical function that can extract data from a specified column from a multidimensional array. This function is very useful when generating a tag list, such as extracting tags from all articles from a list of articles, or extracting user's interest tags from user data.

This article will use a simple example to introduce how to use array_column to quickly extract data and generate a tag list.

1. What is array_column ?

array_column is a built-in function introduced in PHP 5.5.0. It is used to extract the value of a column from a multidimensional array and return an array containing the data of that column.

2. Function syntax

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

  • $column_key : The key name (or index) of the column to be extracted.

  • $index_key (optional): Used as the key name to return the array. If not passed, the returned array will use the default numeric index.

3. Example: Extract the tag list

Suppose we have a multidimensional array containing article data, each article has multiple labels. Our goal is to extract the tags for all articles through array_column and generate a list of tags.

 <?php

// Sample data:Article list,Each article contains title and tags
$articles = [
    [
        'id' => 1,
        'title' => 'PHPTutorial',
        'tags' => ['PHP', 'programming', 'WebDevelopment']
    ],
    [
        'id' => 2,
        'title' => 'JavaScriptgetting Started',
        'tags' => ['JavaScript', 'programming', 'front end']
    ],
    [
        'id' => 3,
        'title' => 'PythonData Analysis',
        'tags' => ['Python', 'Data Analysis', 'Machine Learning']
    ]
];

// use array_column Extract all articles tags
$tags = array_column($articles, 'tags');

// Display the extracted tags
echo '<pre>';
print_r($tags);
echo '</pre>';

?>

4. Process the extracted tags

The result extracted by array_column is an array containing all article tags. However, generally, what we don't want to see is a two-dimensional array (the labels for each post are an array), but instead merge all the labels together to form a flat list of labels. To achieve this, we can flatten the multidimensional array using array_merge and call_user_func_array functions.

 <?php

// Flatten the 2D label array into a one-dimensional array
$flatTags = call_user_func_array('array_merge', $tags);

// Go to the heavy:Remove duplicate tags
$uniqueTags = array_unique($flatTags);

// Show the final label list
echo '<pre>';
print_r($uniqueTags);
echo '</pre>';

?>

5. Sample output

Execute the above code and the final tag list will look like this:

 Array
(
    [0] => PHP
    [1] => programming
    [2] => WebDevelopment
    [3] => JavaScript
    [4] => front end
    [5] => Python
    [6] => Data Analysis
    [7] => Machine Learning
)

These tags are extracted from the tags for each article and are deduplicated through array_unique to ensure that each tag appears only once.

6. Summary

Through the array_column function, we can extract data from specific columns very conveniently from multidimensional arrays. In practical applications, combining other functions (such as array_merge and array_unique ) can quickly process the extracted data, generate useful tag lists, and thus provide users with more personalized recommendations or data analysis.

In this way, PHP provides powerful and efficient array processing capabilities, allowing developers to process complex data structures more conveniently.