Current Location: Home> Latest Articles> How to Use array_fill_keys to Create an Initially Empty User Permission Table? A Practical PHP Guide

How to Use array_fill_keys to Create an Initially Empty User Permission Table? A Practical PHP Guide

gitbox 2025-08-27
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Preface: This section is unrelated to the main content and can be used for initialization code or debugging information</span></span><span>
</span><span><span class="hljs-title function_ invoke__">ini_set</span></span><span>(</span><span><span class="hljs-string">&#039;display_errors&#039;</span></span><span>, </span><span><span class="hljs-number">1</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">error_reporting</span></span><span>(E_ALL);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Initialization complete, preparing to display article content...\n"</span></span><span>;
<p></span>?></p>
<p><hr></p>
<p><?php<br>
// Main content starts<br>
echo "<h1>How to Use array_fill_keys to Create an Initially Empty User Permission Table? A Practical PHP Guide</h1>";</p>
<p>echo <span><span class="hljs-string">"<p>In PHP development, we often need to initialize a permissions table for a group of users or roles. Suppose we have a user list where each user’s initial permissions are empty (or set to null by default). How can we efficiently generate such an array? PHP provides a very convenient function: <code>array_fill_keys
";

echo "

2. Example: Creating a User Permission Table

"
;
echo "

Let’s say we have the following users:

"
;
echo "
$users = ['alice', 'bob', 'charlie'];
"
;

echo "

We want to initialize an empty permissions array for each user:

"
;
echo "
$permissions = array_fill_keys($users, []);
"
;

echo "

After execution, the contents of $permissions are as follows:

"
;
echo "
"</span></span><span>;<br>
</span><span><span class="function_ invoke__">print_r</span></span><span>(</span><span><span>$permissions</span></span><span>);<br>
</span><span><span>echo</span></span><span> </span><span><span>"
"
;

echo "

3. Dynamically Assigning Initial Permissions

"
;
echo "

If we want each user’s initial permissions to be a default value instead of an empty array, for example, granting a 'read' permission:

"
;
echo "
$permissions = array_fill_keys($users, ['read']);
"
;

echo "

The result will be:

"
;
echo "
"</span></span><span>;<br>
</span><span><span class="function_ invoke__">print_r</span></span><span>(</span><span><span>$permissions</span></span><span>);<br>
</span><span><span>echo</span></span><span> </span><span><span>"
"
;

echo "

4. Summary

"
;
echo "

With array_fill_keys, we can quickly create an array where the keys are usernames (or role names) and the values are initial permissions. This saves us from the hassle of manually assigning values through loops, making it highly suitable for initializing user permission tables.

"
;
?>