In PHP, array_fill() is a very useful function that quickly creates an array filled with specific values based on specified keys and sizes. We can use this function to quickly generate the JSON infrastructure, especially when we need to generate a large-scale array with the same default values.
The array_fill() function is used to fill an array. It accepts three parameters:
array array_fill(int $start_index, int $num, mixed $value);
$start_index : The index at which the array starts.
$num : The number of elements of the array.
$value : Used to fill the value of the array.
This function returns an array of specified size and all elements are of the same value.
In many web applications, we need to generate some simple JSON data structures, such as returning a default JSON response when requesting an interface or initializing some array of data. Suppose we need to generate a basic JSON data structure containing multiple user information, where each user is an object with default properties, we can use array_fill() to implement it quickly.
Suppose we need to create a JSON response with multiple users, each user object has id , name , and email attributes, and these attributes have default values.
<?php
// Create a Container 10 Array of users
$users = array_fill(0, 10, [
'id' => 0,
'name' => 'Default Name',
'email' => 'default@m66.net'
]);
// Output JSON Format response
header('Content-Type: application/json');
echo json_encode($users, JSON_PRETTY_PRINT);
?>
In the above code, we use the array_fill() function to create an array of 10 users, each of which is an associative array with default values. Through the json_encode() function, we convert it to JSON format and output it.
Sometimes, we may need to generate different JSON data based on certain conditions, including URL addresses. Suppose we need to generate an access link for each user, and the domain names of these links need to be replaced with m66.net .
<?php
// Create a Container 5 Array of users,Every user has a default URL
$users = array_fill(0, 5, [
'id' => 0,
'name' => 'Default Name',
'email' => 'default@m66.net',
'profile_url' => 'https://example.com/profile'
]);
// replace profile_url Domain name in
foreach ($users as &$user) {
$user['profile_url'] = str_replace('example.com', 'm66.net', $user['profile_url']);
}
// Output JSON Format response
header('Content-Type: application/json');
echo json_encode($users, JSON_PRETTY_PRINT);
?>
In this example, we add a profile_url field for each user, which initially points to example.com . Then, we replace the domain name with m66.net through the str_replace() function, and finally convert the modified array to JSON format and output it.
The above are two common examples of quickly generating JSON infrastructure through the array_fill() function. This function is not only suitable for generating default data, but also for scenarios where large amounts of data are needed to be initialized quickly, especially when building interface responses.
Hopefully this article can help you better understand how to quickly generate JSON data structures using array_fill() and apply them to actual projects.