如何转换数组中的数字值?使用php


How to transform number values from array? Using php

我有一个网站(site-1(,它从其他网站(site-2(导入页面。

这些页面在站点 2 中有一个 ID 号,导入完成后,此编号将复制到站点 1。目前为止,一切都好。

问题是站点 2 的 id 很大,例如:32423201639、3212450421639,...而站点-1中的系统无法处理它们。因此,我需要在导入完成后缩小这些数字。

重要的是:

  • 以生成唯一的数值。

  • 大于 3000 且小于 10000。

  • 它不能使用 rand((。如果我们多次执行此操作,则结果必须相同

更新请记住:

此导入每周完成一次,所以我需要考虑这一点:假设第一次导入完成,然后在第二次导入中,只有第一个数组值发生变化,但其他数组值仍然存在,那么这个将是唯一要更改的数组值,另一个将保持与第一次导入相同的值。

我想的第一件事是这样的(但最重要的是丢失(:

$array_values_site1 = array("12345" , "123456", "1234567", "12345678", "123456789", "1234567890", "12345678901", "123456789012", "1234567890123", "12345678901234", "123456789012345", "1234567890123456");
$array_values_site2 = array();
foreach ($array_values_site1 as &$value) {

    /* here I need to change the value of $value:
    --- to be bigger than 3000 and smaller than 10000.
    --- It can not use rand(). If we execute this several time the results must be the same
    --- to be unique */ 
    $new_value = "....";
    $array_values_site2 [] = $new_value;
}

查看注释,对原始 ID 进行哈希处理看起来最好:

$hashids = new Hashids'Hashids('this is my salt');
$id = $hashids->encode(1);
$original = $hashids->decode($id);

要指定要在结果中使用的最小长度(不是数字,而是长度(和字符,请包括第二个和第三个参数:

$hashids = new Hashids'Hashids('this is my salt');
$id = $hashids->encode(1, 8, 'abcdefghij1234567890');
$original = $hashids->decode($id);
// $id = '514cdi42';

有关信息,请参阅 hashids.org 和 github。