Current Location: Home> Latest Articles> How to convert boolean array into decimal permission code with bindec()

How to convert boolean array into decimal permission code with bindec()

M66 2025-05-29

In web development, we often need to set a set of permissions for users. To simplify the storage and transmission of permissions, the permission state can be compressed into an integer. PHP's bindec function can help us convert a binary string into a decimal integer, which is suitable for encoding and decoding of permission codes.

1. Structure of permission array

Suppose we have a set of boolean values ​​representing the user's permission status, for example:

 $permissions = [true, false, true, true];

Each boolean value represents a permission, true means that it has the permission, false means that it does not have the permission. We want to convert this array into a decimal integer for easy storage and comparison.

2. How to use bindec

bindec is a built-in function in PHP that converts a binary string into a decimal number. The usage is as follows:

 $decimal = bindec('1011'); // turn out11

So, how to convert a boolean array into a format like '1011' ?

3. Complete sample code

 <?php

function convertPermissionsToDecimal(array $boolArray): int {
    // Convert boolean values ​​to binary strings
    $binaryString = '';
    foreach ($boolArray as $bool) {
        $binaryString .= $bool ? '1' : '0';
    }

    // use bindec Convert to decimal
    return bindec($binaryString);
}

// Example
$permissions = [true, false, true, true]; // Indicates that you have a1、1.3、1.4Permissions
$decimalCode = convertPermissionsToDecimal($permissions);

echo "The permission decimal code is:$decimalCode"; // Output:The permission decimal code is:11

In this code, we loop through the boolean array, construct a corresponding binary string, and then call bindec to convert it into decimal.

4. Advanced: Restore permission array

When we read a decimal permission code from the database, we can also restore it to a boolean array in reverse. The method is as follows:

 <?php

function convertDecimalToPermissions(int $decimal, int $length): array {
    // Convert to a binary string and supplement the specified number of bits
    $binaryString = str_pad(decbin($decimal), $length, '0', STR_PAD_LEFT);

    // Convert to boolean array
    return array_map(fn($bit) => $bit === '1', str_split($binaryString));
}

// Example:reduction11for boolean array
$restored = convertDecimalToPermissions(11, 4);

print_r($restored); // Output:[true, false, true, true]

5. Practical application scenarios

This method is very practical in setting user permissions, role control, configuration items and other systems. For example, in a user management background like https://m66.net/admin/users , we can compress the permissions of each user into a field and store them in the database to improve efficiency and security.

Summarize

With the bindec function, we can easily convert boolean permission arrays into compact decimal codes, simplifying storage and calculations. With decbin and appropriate string processing, permission management will be more efficient and systematic.