Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:1个int变量中包含3个int ID_Php - Fatal编程技术网

PHP:1个int变量中包含3个int ID

PHP:1个int变量中包含3个int ID,php,Php,我必须在一个名为$id的数字字符串中传递3个变量(int)。为此,我使用填充创建$id,然后将填充分解得到变量。它必须是数字,否则我会在变量之间使用下划线。我使用11个零作为填充,因为我知道变量不会有那么多的零。所以现在如果我有: $int_one = 1; $int_two = 2; $int_three = 3; 这将是: $id = "1000000000002000000000003"; 要创建新Id,我使用: $id = $int_one . "00000000000" . $in

我必须在一个名为$id的数字字符串中传递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;
$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]);
}
要分离我使用的Id,请执行以下操作:

$id = $int_one . "00000000000" . $int_two . "00000000000" . $int_three;
$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变量

有没有办法克服这个问题,还是需要重新思考

是的,你需要重新考虑一下。你为什么要这样做?只需创建一个包含三个参数的函数,并传入三个整数:

function foo($int1, $int2, $int3) {
}
您的示例使用字符串,而不是整数,因此您甚至没有遵循自己的要求

有没有办法克服这个问题,还是需要重新思考

是的,你需要重新考虑一下。你为什么要这样做?只需创建一个包含三个参数的函数,并传入三个整数:

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

您的示例使用字符串,而不是整数,因此您甚至没有按照自己的要求进行操作。

您可以尝试以下方法:

$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;

您可以尝试以下方法:

$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;

您可以使用一些字符串魔术来确保任何数字在一行中都不超过一个零,并使用“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

您可以使用一些字符串魔术来确保任何数字在一行中都不超过一个零,并使用“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


你真的是指一个整数,还是仅仅是一个数字字符串?int(在32位计算机上)限制为32位,最多总共9位,因此填充将使其溢出。是否使用不同的填充?比如123456789,或者如果你想要更大的数字987654321,如果你在某个时候需要这个数字呢?输入和输出的三个变量需要是int。但是是的,$id只需要是一个数字字符串。我会更具体地更新这篇文章。这个问题对int有一个要求。虽然我不知道这个决定背后的原因——这是他问的。你真的是指int,还是仅仅是一个数字字符串?int(在32位计算机上)限制为32位,最多总共9位,因此填充将使其溢出。是否使用不同的填充?比如123456789,或者如果你想要更大的数字987654321,如果你在某个时候需要这个数字呢?输入和输出的三个变量需要是int。但是是的,$id只需要是一个数字字符串。我会更具体地更新帖子。这个问题需要int。虽然我不知道这个决定背后的原因——这就是他问的。是的,不幸的是,必须这样做。我需要一个int$id变量,它可以被拆分为三个int变量,可以是0。你没有给出这样做的理由,你能把实际使用这个过程的代码添加到你的问题中吗?是的,很遗憾,它必须这样做。我需要一个int$id变量,它可以被分成三个int变量,可以是0。你不需要解释为什么必须这样做,你能把实际使用这个过程的代码添加到你的问题中吗?如果输入变量是max int 2147483647,这行吗?你总是可以用一个更大的数字进行乘法。。。这只是为了说明原理。如果输入变量是max int 2147483647,这会起作用吗?您总是可以使用更大的数字进行乘法。。。这只是为了表明原则。