I have numerous 15 digit numbers. I need to rearrange the digits in each number based on a set order to hide the composite identifiers. for exaample: 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.
Implode the array and cast it into an int.
(int) impode('',$array)
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);
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