0

$id という 1 つの数値文字列内で 3 つの変数 (int) を渡す必要があります。これを行うために、変数を取得するために分解できるパディングを使用して $id を作成しています。それ以外の場合は、変数の間にアンダースコアを使用します。変数にはそれほど多くのゼロがないことがわかっているので、パディングとして 11 個のゼロを使用しています。したがって、現在私が持っている場合:

$int_one = 1;
$int_two = 2;
$int_three = 3;

それは次のようになります。

$id = "1000000000002000000000003";

私が使用する新しいIDを作成するには:

$id = $int_one . "00000000000" . $int_two . "00000000000" . $int_three;

そして、私が使用するIDを分離するには:

$int_one = 0;
$int_two = 0;
$int_three = 0;
if (strpos($id,"00000000000") !== false) {
    $id = strrev($id); // Reversed so 0's in int's don't get counted
    $id = explode("00000000000", $id);
    // Set numbers back the right way
    $int_one = strrev($id[2]);
    $int_two = strrev($id[1]);
    $int_three = strrev($id[0]);
}

これは、個々の変数が 0 の場合に問題が発生します。これを克服する方法はありますか、それとも大幅な再考が必要ですか?

編集: $id intではなく数値文字列であると想定されています

0 ~ 2147483647 の int 変数を処理する必要がある

4

3 に答える 3

2

文字列マジックを使用して、連続するゼロが 1 つしかないことを確認し、「00」を使用して値を区切ることができます。これにより、int のサイズや構成に関係なく一意にデコードできる数値文字列が生成されます。

$a = 100;
$b = 0;
$c = 120;

// Encode;

$id = str_replace('0', '01', $a).'00'
     .str_replace('0', '01', $b).'00'
     .str_replace('0', '01', $c);

// $id = "101010001001201"

// Decode;

$tmp = split('00', $id);
$a2 = intval(str_replace('01', '0', $tmp[0]));
$b2 = intval(str_replace('01', '0', $tmp[1]));
$c2 = intval(str_replace('01', '0', $tmp[2]));

// $a2 = 100, $b2 = 0, $c2 = 120
于 2013-01-13T11:52:33.110 に答える
1

これを克服する方法はありますか、それとも大幅な再考が必要ですか?

はい、考え直す必要があります。なんでそんなことする必要があるの?3 つのパラメーターを持つ関数を作成し、3 つの int を渡すだけです。

function foo($int1, $int2, $int3) {
}

ちなみに、あなたの例ではintではなく文字列を使用しているため、独自の要件にも従っていません。

于 2013-01-13T11:27:09.420 に答える
0

この方法を試すことができます:

$int_one = 1;
$int_two = 2;
$int_three = 3;

$id = $int_one * 1000000000000 + $int_two * 1000000 + $int_three;
// This will create a value of 1000002000003

プロセスを逆にするには:

// Get the modulo of $id / 1000000 --> 3
$int_three = $id % 1000000;

// Recalculate the base id - if you would like to retain the original id, first duplicate variable
// This would make $id = 1000002;
$id = ($id - $int_three) / 1000000;

// Again, get modulo --> 2
$int_two = $id % 1000000;

// Recalculate base id
$id = ($id - $int_two) / 1000000;

// Your first integer is the result of this division.
$int_one = $id;
于 2013-01-13T11:42:43.913 に答える