问题描述:

From what I've read, you can store bytes in an array in PHP with this sort of command:

 $array = [1,2,14,10];

I have four basic flags I want to add to each array value, something like 0000. If the user performed an action that unlocked the 3rd flag, the value should look like 0010. If all flags are set, the value would look like 1111.

I plan on having a lot of these types of array values, so I was wondering what the smallest possible value I could put into an array that's also Java friendly? After the data is stored in PHP, I'll need to get the array in Java and be able to retrieve these flags. That might look something like:

somevar array = array_from_php;

if(array[0][flag3] == 1)//Player has unlocked this flag

/*do something */

Any advice is greatly appreciated.

网友答案:

I think you dont want an array but an byte (8 bit) or a word (16 bit) or a dword (32 bit) to store your flags in RAM or persistent in DB or textfile.

While PHP is not a type save language you cannot declare those types as far as I know. But you inspired me. The PHP's error_reporting value is stored like this. But I think it is a full integer instead of just a byte, word or dword.

I did a little test and it seems to work:

<?php

// Flag definitions
$defs = array(
    "opt1" => 1,
    "opt2" => 2,
    "opt3" => 4,
    "opt4" => 8,
    "opt5" => 16,
    "opt6" => 32
);

// enable flag 1,3 and 4 by using a bitwise "OR" Operator
$test = $defs["opt1"] | $defs["opt3"] | $defs["opt4"];
displayFlags($test, $defs);

// enable flag 6, too
$test |= $defs["opt6"];
displayFlags($test, $defs);

// disable flag 3
$test &= ~$defs["opt3"];
displayFlags($test, $defs);

// little improvement: the enableFlag/disableFlag functions
enableFlag($test, $defs["opt5"]);
displayFlags($test, $defs);

disableFlag($test, $defs["opt5"]);
displayFlags($test, $defs);

function displayFlags($storage, $defs) {   

    echo "The current storage value is: ".$storage;
    echo "<br />";

    foreach($defs as $k => $v) {
        $isset = (($storage & $v) === $v);
        echo "Flag \"$k\" : ". (($isset)?"Yes":"No");
        echo "<br />";
    }

    echo "<br />";
}

function enableFlag(&$storage, $def) {
    $storage |= $def;
}

function disableFlag(&$storage, $def) {
    $storage &= ~$def;
}

The output is:

The current storage value is: 13
Flag "opt1" : Yes
Flag "opt2" : No
Flag "opt3" : Yes
Flag "opt4" : Yes
Flag "opt5" : No
Flag "opt6" : No

The current storage value is: 45
Flag "opt1" : Yes
Flag "opt2" : No
Flag "opt3" : Yes
Flag "opt4" : Yes
Flag "opt5" : No
Flag "opt6" : Yes

The current storage value is: 41
Flag "opt1" : Yes
Flag "opt2" : No
Flag "opt3" : No
Flag "opt4" : Yes
Flag "opt5" : No
Flag "opt6" : Yes

The current storage value is: 57
Flag "opt1" : Yes
Flag "opt2" : No
Flag "opt3" : No
Flag "opt4" : Yes
Flag "opt5" : Yes
Flag "opt6" : Yes

The current storage value is: 41
Flag "opt1" : Yes
Flag "opt2" : No
Flag "opt3" : No
Flag "opt4" : Yes
Flag "opt5" : No
Flag "opt6" : Yes

Conclusion:

I think this is the most efficient way to store flags with a minimum of space. But if you store it like this in a database you may get problems with efficient queries on those flags. I dont think that it is possible to query one or more specific bits of an integer value. But maybe I am wrong and you can use bitwise operator in a query, too. However, I love this kind of saving data.

网友答案:

Java also has a byte[], which will be the smallest storage as well. With that said, I believe you can find what you are looking for in this post: Store binary sequence in byte array?

相关阅读:
Top