doulang7699 2016-05-15 06:18
浏览 63
已采纳

数字多维数组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>";
    }
  • 写回答

2条回答 默认 最新

  • douwuying4709 2016-05-15 06:25
    关注

    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 ) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料