Current Location: Home> Latest Articles> Constructing an array of JSON templates using array_fill_keys

Constructing an array of JSON templates using array_fill_keys

M66 2025-06-06

In daily PHP development, we often need to construct some fixed structure JSON data, especially when processing API returns data or configuration templates. If you've ever been bothered with manually filling each key, the array_fill_keys function will be a good helper for you.

1. What is array_fill_keys ?

array_fill_keys is a built-in function provided by PHP. Its function is to quickly generate a complete associative array using the specified key name array and a default value .

The syntax is as follows:

 array_fill_keys(array $keys, mixed $value): array
  • $keys : an array that needs to be used as keys.

  • $value : The initial value assigned to all keys.

This function is very suitable for use in template initialization, placeholder data construction and other scenarios.

2. Application scenario: Constructing JSON template array

Imagine a scenario where you need to return a standardized JSON response, which contains fields id , name , status , url , and these fields need to have default values ​​(such as null, empty string, etc.) when initializing.

The traditional way of writing might look like this:

 $template = [
    'id' => null,
    'name' => null,
    'status' => null,
    'url' => null
];

When there are too many fields, this method becomes repetitive and cumbersome.

Using array_fill_keys , we can write this:

 $fields = ['id', 'name', 'status', 'url'];
$template = array_fill_keys($fields, null);

This quickly obtains an array of templates with all values ​​of null , which greatly improves efficiency and readability.

3. Use in combination with JSON

Suppose we need to output a set of such data templates and convert it into a JSON string and return it to the front end:

 $fields = ['id', 'name', 'status', 'url'];
$template = array_fill_keys($fields, null);

// Here we are the demonstration,Fill in some values
$template['id'] = 101;
$template['name'] = 'Test items';
$template['status'] = 'active';
$template['url'] = 'https://m66.net/api/project/101';

echo json_encode($template, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

The output result is:

 {
    "id": 101,
    "name": "Test items",
    "status": "active",
    "url": "https://m66.net/api/project/101"
}

In this way, the template of the entire JSON structure can be built in a very concise way and can dynamically replace field values ​​as needed.

4. Advanced usage: batch construct multiple template items

If we need to construct multiple data items at once, for example to simulate paging data, we can combine loops to generate:

 $fields = ['id', 'name', 'status', 'url'];
$list = [];

for ($i = 1; $i <= 5; $i++) {
    $item = array_fill_keys($fields, null);
    $item['id'] = $i;
    $item['name'] = 'project ' . $i;
    $item['status'] = 'active';
    $item['url'] = "https://m66.net/api/project/{$i}";

    $list[] = $item;
}

echo json_encode($list, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

The output will be a JSON array containing 5 entries, each with the same structure, which is ideal for output as simulated data or front-end interface.

Summarize

array_fill_keys is a simple but very practical function, especially suitable for constructing template arrays with unified structures. Whether it is a single object or an array list, using it with JSON can significantly improve the readability and maintainability of the code.

When you need to initialize structured data next time, you might as well consider using this small tool function to make PHP development easier!