Tsv. M. Tsv. M. - 6 months ago 16
PHP Question

Numeric multidimensional array php

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>";
}

Answer

if you have a matrix should check with 2 for like following code:

for($i = 1; $i <= 11; $i++)
{
     for($j = 1; $j <= 11; $j++)
     {
         if($array[$i][$j] == 'p') echo "row=$i and col = $j";
     }
}

in the above example i check all rows and rows cols

use from this function

function array_search2($needle, $haystack){
     $i = 0;
     foreach($haystack as $key => $value)
     {
         $j = 0;
         foreach($value as $key2 => $value2)
         {
              if($value2 == $needle)
                   return array($i, $j);

               $j++;
         }
         $i++;
     }
     return false;
}

this function will return an array contain row and col of found status or false;

i tested with following data:

$array = array( 0 => array(0 => 'a', 1=> 'b'), 1 => array(0 => 'c', 1=> 'd'));
print_r(array_search2('d', $array));
// out put Array ( [0] => 1 [1] => 1 )