Current Location: Home> Latest Articles> How to initialize the default array of API request parameters using PHP's array_fill_keys function?

How to initialize the default array of API request parameters using PHP's array_fill_keys function?

M66 2025-05-17

When developing PHP interface requests, we often need to process incoming parameters and set default values ​​for certain parameters. If there are many parameters, manually initializing the array may appear redundant and prone to errors. At this time, array_fill_keys() becomes a very efficient tool.

This article will introduce how to use array_fill_keys() to quickly initialize the default array of API request parameters to improve the maintainability and clarity of the code.

What is array_fill_keys() ?

array_fill_keys() is a built-in function in PHP that pairs an array's keys with a unified value to generate a new array.

grammar:

 array_fill_keys(array $keys, mixed $value): array
  • $keys : an array used as the new array key name.

  • $value : The default value for all keys.

Scenario example: API request parameter initialization

Imagine you are developing an interface for querying article data, and the client may pass in the following parameters:

  • page : Current page number

  • limit : Number per page

  • sort : sort fields

  • order : sorting direction (asc or desc)

In order to avoid system errors caused by user failure to pass these parameters, the background should set a default parameter array. We can use array_fill_keys() to simplify this process.

Sample code:

 <?php

// Define parameter key names
$paramKeys = ['page', 'limit', 'sort', 'order'];

// use array_fill_keys Initialize the default value
$defaultParams = array_fill_keys($paramKeys, null);

// Manually overwrite default values(If needed)
$defaultParams['page'] = 1;
$defaultParams['limit'] = 10;
$defaultParams['sort'] = 'created_at';
$defaultParams['order'] = 'desc';

// Simulate to get the incoming parameters of the client
$userInput = [
    'page' => 2,
    'order' => 'asc'
];

// Merge user input with default values(User input priority)
$requestParams = array_merge($defaultParams, $userInput);

// Output the final parameter array
print_r($requestParams);

Output result:

 Array
(
    [page] => 2
    [limit] => 10
    [sort] => created_at
    [order] => asc
)

As you can see, array_fill_keys() allows us to batch initialize a clean default parameter structure by simply listing the required parameter names, greatly reducing the possibility of manual input errors.

Practical application: Request a remote API

Suppose we want to send these parameters to the remote interface https://api.m66.net/articles/list via POST request, we can do this:

 <?php

$apiUrl = 'https://api.m66.net/articles/list';

$options = [
    'http' => [
        'method'  => 'POST',
        'header'  => "Content-Type: application/x-www-form-urlencoded\r\n",
        'content' => http_build_query($requestParams),
        'timeout' => 10
    ]
];

$context  = stream_context_create($options);
$result = file_get_contents($apiUrl, false, $context);

echo $result;

summary

Using array_fill_keys() to initialize the default values ​​of API request parameters is not only concise, but also improves the maintainability of the code. When your interface parameter list changes, you only need to update the parameter key array without modifying multiple manual assignment statements.

This is a highly recommended way of practice, especially suitable for parameter management and structural unity in large projects.

  • Related Tags:

    API