1

I am trying to make a Vigenère table using PHP. My goal is to make a big array with 26 smaller arrays in it like this:

$bigarray = [['a'-'z']['b'-'a']...['y'-'x']['z'-'y']];

I'm thinking of making the first array using the range() function, append that in the big array, then use a for loop to take the first letter, place that letter at the end and make that array append in a big array x25

$letterarray = range('a','z');
array_merge($bigarray, $firstarray);

for ($idx = 0; $idx < 26; $idx++) {
    $letterarray = /* Take first letter from $letterarray, put that letter in the end. */
    $bigarray = /* Put the $letterarray into the $bigarray. */

I don't know if I need to use the array_splice() or array_slice() function. I also don't know how to put the small array into the big array while keeping the 'array in array' form, because array_merge() just shoves every value into one array.

Cœur
  • 32,421
  • 21
  • 173
  • 232
EdKorket
  • 48
  • 10

2 Answers2

1

Your approach is solid. To execute, you just need to copy the previous array, and then use array_shift and array_push to "cycle" it.

$bigarray = [range('a','z')];
for( $i=1; $i<26; $i++) {
    // $i=1 because we already have the first one.
    $copy = $bigarray[$i-1]; // get most recent entry
    array_push($copy,array_shift($copy));
    $bigarray[$i] = $copy;
}
Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
0

Thanks for your comment, after I wrote this thread I figured out a way myself.

$bigarray = array();
$alphas = range('a', 'z');
$bigarray[0] = $alphas;

for ($idx = 1; $idx <= 25; $idx++) {
    $firstletter = $alphas[0];
    $alphas = array_slice($alphas,1);
    array_push($alphas, $firstletter);
    $bigarray[$idx] = $alphas;
}

It stores the first letter of the [a-z] array ($alphas) in the variable $firstletter, slices the $alphas array and pushes the element in the $firstletter varible at the end and stores the new array [b-a] into the $bigarray.

The neat thing is that array_slice just changes the indices automatically. Thanks for the comment :)

-Ed

EdKorket
  • 48
  • 10