0

I have numerous 15-digit numbers. I need to rearrange the digits in each string based on a set order to hide the composite identifiers. For example: convert 123456789123456 to 223134897616545

The method I am thinking of is:

  1. Extract each digit using $array = str_split($int)
  2. Create a new array with required order from $array above

But here is where I am stuck. How do I combine all digits of array to single integer? Also is this an efficient way to do this?

Also I need to know the order in which it was shuffled so as to retrieve the original number.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
FBP
  • 345
  • 3
  • 15

4 Answers4

0

You are already half way. You can use join or implode:

$no = 123456789123456;
$no_arr = str_split($no);
shuffle($no_arr);
echo join($no_arr);   // implode($no_arr);

I get this randomly:

574146563298123

Resultant is string. You can convert to int by type casting: echo (int) join($no_arr);

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

another aproach in maybe less lines:

$num = 1234;
$stringOriginal = (string) $num;
// build the new string, apply your algorithm for that as needed
$stringNew = $stringOriginal[1] . $stringOriginal[0] . $stringOriginal[3] . $stringOriginal[2];
$numNew = (int) $stringNew;

warning: this has no error handling, it's easy to mess up the indices and get an error because of that

cypherabe
  • 2,562
  • 1
  • 20
  • 35
0

This appears to be a task of obfuscation (not encryption) -- meaning that you only want to obscure the value or make it slightly harder to guess. The truth is that if you are not changing any of the characters, but merely shuffling them around, then a brute force technique can very easily try every combination of the characters in order to "happen upon" the correct one.

Security aside, use a 15-element array to re-map the characters. How you generate and store this map is up to you. Use the map to revert the shuffled string to its original value.

Code: (Demo)

$input = '123456789123456';

$map = [5, 8, 13, 2, 4, 11, 0, 14, 1, 7, 3, 12, 10, 6, 9];

$result = '';
foreach ($map as $offset) {
    $result .= $input[$offset];
}
echo "Rearranged: $result\n---\n";

$reverted = '';
asort($map);
foreach ($map as $offset => $notUsed) {
    $reverted .= $result[$offset];
}
echo "Reverted: $reverted";

Output:

Rearranged: 695353162844271
---
Reverted: 123456789123456
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-1

Implode the array and cast it into an int.

(int) implode('',$array)

Ryan M
  • 18,333
  • 31
  • 67
  • 74
rollingBalls
  • 1,808
  • 1
  • 14
  • 25
  • `implode()`'s default glue is an empty string. Casting as an integer will potentially destroy characters when one or more zeros are moved to the front of the string. – mickmackusa Aug 07 '22 at 22:53
  • Casting is perfectly fine for the use-case described, he/she didn't mention anything about padded-zeros. And even if there are '0001' -> 1 is exactly the desired behavior for them, as they want integers and integers don't zero-pad. – rollingBalls Aug 09 '22 at 13:13