简体   繁体   English

如何在PHP中交换多维数组的矩阵主对角元素

[英]How to swap matrix main diagonal elements of multidimensional array in PHP

I want to swap only main diagonal elements of matrix of an multidimensional array like below 我想只交换多维数组矩阵的主对角线元素,如下所示

Before swapping 在交换之前

 11  12 13  14  15
 21  22 23  24  25
 31  32 33  34  35
 41  42 43  44  45
 51  52 53  54  55

After swapping -it should be
 55 12  13  14  15
 21 44  23  24  25
 31 32  33  34  35
 41 42  43  22  45
 51 52  53  54  11

What i did is - i am able to get diagonal elements as below, its quite simple but i cannot proceed further 我所做的是 - 我能够获得如下的对角线元素,它非常简单,但我无法继续前进

<?php

  $array = array (
        array( '11','12','13','14','15'),
        array( '21','22','23','24','25'),
        array( '31','32','33','34','35'),
        array( '41','42','43','44','45'),
        array( '51','52','53','54','55')
    );

  foreach ( $array as $key => $val ) {

      print "<br>".$val[$key];

   }
?>  

It's sounds like you're a bit lost. 听起来你有点失落。 This isn't too hard, I suggest you dividing it into smaller problems which might make it easier for you to deal with. 这不是太难,我建议你把它分成更小的问题,这可能会让你更容易处理。

  1. First create a function to get the diagonal array based on matrix size 首先创建一个函数,根据矩阵大小得到对角线阵列

    suggestion You have a nxn matrix represented by 2d array a[i][j] then (pseudo code): 建议你有一个由2d数组表示的nxn矩阵a[i][j]然后(伪代码):

     while i < n do: diagonal << a[i][j] i,j+=1 
  2. Then, create a swap function for the diagonal, there's reverse_array function in PHP that might help. 然后,为对角线创建一个交换函数,PHP中的reverse_array函数可能有所帮助。

  3. Finally, when having reversed-diagonal calculated you can just print the old array and swap its diagonal with the new one. 最后,当进行反向对角线计算时,您可以打印旧数组并将其对角线换成新数组。

There are shorter ways and much efficient ones, but in my opinion this how you should do it so next time you're facing this kind of problem you'll know the flow. 有更短的方法和更有效的方法,但在我看来你应该这样做,所以下次你遇到这种问题时你会知道流程。

I think what you are missing is being able to access your $array by both 'x' and 'y' coordinates by: $array[$x][$y]. 我认为你所缺少的是能够通过'x'和'y'坐标访问你的$数组:$ array [$ x] [$ y]。

<?php
  $array = array (
        array( '11','12','13','14','15'),
        array( '21','22','23','24','25'),
        array( '31','32','33','34','35'),
        array( '41','42','43','44','45'),
        array( '51','52','53','54','55')
    );
print_array($array);
echo "--------------\n";

//swap 
for( $i=0; $i<2; $i++ ){
  $temp= $array[$i][$i];
  $array[$i][$i]= $array[4-$i][4-$i];
  $array[4-$i][4-$i]=$temp;
}
print_array($array);

function print_array( $array )
{
  for($i=0;$i<5;$i++)
  {
    for($j=0;$j<5;$j++)
       echo $array[$i][$j]." ";
    echo "\n";
  }
}
?>

I'll let you figure out why the swap loop has $i<2 in it whereas the print loop has $i<5 :) 我会让你弄清楚为什么交换循环中有$i<2 ,而打印循环有$i<5 :)

Yours, TonyWilk 你的,TonyWilk

Here is working example ;) 这是工作示例;)

<?php

$array = array (
            array( '11','12','13','14','15','16'),
            array( '21','22','23','24','25','26'),
            array( '31','32','33','34','35','36'),
            array( '41','42','43','44','45','46'),
            array( '51','52','53','54','55','56'),
            array( '61','62','63','64','65','66')
        );

//print "<pre>";
//print_r($array); exit;

$cnt = count($array);

$i = 0; $j = count($array) - 1;

foreach ( $array as $key => $val ) {

    $newArr[$i][$j] = $val[$key];

    $j--; $i++;

}


$reversed = array_reverse($newArr);

$result = array_replace_recursive($array,$reversed);

matrix($array,$cnt);

print "=======================<br><br>";

matrix($result,$cnt);

function matrix( $result,$cnt )
{
    for($i=0;$i<$cnt;$i++)
    {
        for($k=0;$k<$cnt;$k++)
        {
            echo $result[$i][$k]." ";
            echo "<br>";
        }
    }
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM