I have this numeric multidimensional array in php. It's Playfair matrix and I want to get the keys of a letter from the matrix.
Let's say I have the letter "P", which is on the third row(0 to 4) and second column(0 to 4). I have tried several things array_keys(not working as I want it to), functions to get the key of the first level of the array, making the array one dimensional and doing some magic tricks...Nothing works and I would like some help.
This is the function that gets the keys of the first level:
function array_search2($needle, $haystack){
$l = count($haystack);
for ($i=0; $i < $l; $i++) {
if (in_array($needle, $haystack[$i])) return $i;
}
return false;
}
I want to have this array with letters, to get two letters from it, to get these two letters their coordinates(from the PLayfair matrix) and to comprate their rows/columns. Thank you and I'll post any code that is needed.
Edit : I'm posting all I have:
$keyword = str_replace(' ','', $_POST['keyword']);
$plaintext = str_replace(' ','', $_POST['plaintext']);
$key = str_split($keyword); //string to array
$plain = str_split($plaintext); //string to array
$alphabet = array(
0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f',
6 => 'g', 7=> 'h', 8 => 'i', 9 => 'j', 10 => 'k', 11 => 'l',
12 => 'm', 13 => 'n', 14 => 'o', 15 => 'p', 16 => 'q', 17 => 'r',
18 => 's', 19 => 't', 20 => 'u', 21 => 'v', 22 => 'w', 23 => 'x',
24 => 'y');
for ($i=0; $i < count($key); $i++) {
for ($j=0; $j < count($alphabet) ; $j++) {
if ($keyword[$i] == $alphabet[$j]) {
unset($alphabet[$j]);
}
}
}
$keywordFilpped = array_flip($key);
$alphabetFlipped = array_flip($alphabet);
$mergedFlipped = $keywordFilpped + $alphabetFlipped;
$i = 0;
foreach ($mergedFlipped as $key => $value) {
$mergedFlipped[$key] = $i;
$i++;
}
$merged = array_flip($mergedFlipped);
$index = 0;
for ($i=0; $i < 5; $i++) {
for ($j=0; $j < 5; $j++) {
$matrix[$i][$j] = $merged[$index];
$index++;
}
}
echo "PLAYFAIR MATRIX:<br>";
for ($i=0; $i < 5; $i++) {
for ($j=0; $j < 5; $j++) {
echo $matrix[$i][$j];
}
echo "<br>";
}