Current Location: Home> Latest Articles> How to construct a table column configuration array with default values ​​using PHP's array_fill_keys function?

How to construct a table column configuration array with default values ​​using PHP's array_fill_keys function?

M66 2025-06-06

When developing a background management system, content management platform or data display page, we often need to define the column configuration of the table. For example, we may need to set attributes such as title, alignment, and whether to display for each column. In these scenarios, the array_fill_keys() function can help us quickly construct a configuration array with "default values", greatly simplifying the process of initializing the configuration.

1. What is array_fill_keys?

PHP's array_fill_keys() is a built-in function that combines an array of key names with a unified default value to generate an associative array.

grammar:

 array_fill_keys(array $keys, mixed $value): array
  • $keys : an array of key names to use.

  • $value : The default value specified for each key.

2. Actual scenario: Construct table column configuration array

Suppose we have a data table that needs to display the following fields:

 $columns = ['id', 'username', 'email', 'status', 'created_at'];

We want to provide a unified initial configuration for these fields, such as:

 [
    'title' => '',
    'align' => 'left',
    'visible' => true
]

Implementation using array_fill_keys:

 <?php

$columns = ['id', 'username', 'email', 'status', 'created_at'];

$defaultConfig = [
    'title' => '',
    'align' => 'left',
    'visible' => true
];

// usearray_fill_keysConstruct a configuration array with default values
$configs = array_fill_keys($columns, $defaultConfig);

// Example:Override the default configuration for specific fields
$configs['id']['title'] = 'serial number';
$configs['username']['title'] = 'username';
$configs['email']['title'] = 'Mail';
$configs['status']['title'] = 'state';
$configs['created_at']['title'] = 'Creation time';

// Example输出
echo '<pre>';
print_r($configs);
echo '</pre>';

Output result:

 Array
(
    [id] => Array
        (
            [title] => serial number
            [align] => left
            [visible] => 1
        )

    [username] => Array
        (
            [title] => username
            [align] => left
            [visible] => 1
        )

    ...
)

3. Combined with actual conditions: Front-end page reading configuration

Suppose you want to pass these column configurations to the front end through the interface, such as an address:

 https://m66.net/api/table/config

You can convert $configs to JSON and output it to the front end:

 header('Content-Type: application/json');
echo json_encode($configs);

The front-end can dynamically render tables based on these configurations.

4. Tips

  • If you have multiple fields with different configuration structures, you can also combine array_map() or manually override the default value;

  • It can be further encapsulated into a function for easy multiplexing;

  • Note: array_fill_keys() is a shallow copy. If you modify the value of a certain key, the original $defaultConfig will not change, but be careful when multiple keys share the same array reference (can be avoided by array_map() + array_merge() ).