简体   繁体   English

如何交换多维数组中的最大和最小数字?

[英]How to swap largest and smallest number in multidimensional array?

I'm writing a program that swaps largest and smallest number in multidimensional array.我正在编写一个交换多维数组中最大和最小数字的程序。 Largest number should be on the place where is the smallest, and smallest where is the largest, and the idea is to use pure logic, without any php function that could help me out.最大的数字应该在最小的地方,最小的地方应该是最大的,想法是使用纯逻辑,没有任何可以帮助我的 php function。

Please check my code and help me about this problem.请检查我的代码并帮助我解决这个问题。

For instance:例如:

$array = [
  [45, 456, 321, 344, 567],
  [100, 434, 173, 400, 789],
  [191, 211, 457, 809, 900],
  [431, 323, 432, 805, 906],
  [708, 232, 897, 101, 696]
];

New order should be:新订单应该是:

$array = [
  [906, 456, 321, 344, 567],
  [100, 434, 173, 400, 789],
  [191, 211, 457, 809, 900],
  [431, 323, 432, 805, 45],
  [708, 232, 897, 101, 696]
]

I've tried with adding and changing code with this piece of code, but it doesn't give me right results...我试过用这段代码添加和更改代码,但它没有给我正确的结果......

$min_index = $max_index = 0;
foreach($array as $k => $v){
    if($v < $array[$min_index]){
        $min_index = $k;
    }
    if($v > $array[$max_index]){
        $max_index = $k;
    }
}
$min = $array[$min_index];
$array[$min_index] = $array[$max_index];
$array[$max_index] = $min;
$array = [
  [45, 456, 321, 344, 567],
  [100, 434, 173, 400, 789],
  [191, 211, 457, 809, 900],
  [431, 323, 432, 805, 906],
  [708, 232, 897, 101, 696]
];

$intRows = 4;
$intCols = 4;

$intMaxRow = $intMinRow = $intMaxCol = $intMinCol = 0;

$minIndex = $maxIndex = 1;

for($row = 0; $row < $intRows; $row++)
{
  for($col = 0; $col < $intCols; $col++)
  {
    if($array[$row][$col] > $maxIndex)
    {
      $maxIndex = $array[$row][$col];
      $intMaxRow = $row;
      $intMaxCol = $col;
    }
    if($array[$row][$col] < $minIndex)
    {
      $minIndex = $array[$row][$col];
      $intMinRow = $row;
      $intMinCol = $col;
    }
  }
}

$arrNxm[$intMinRow][$intMinCol] = $minIndex;
$arrNxm[$intMaxRow][$intMaxCol] = $maxIndex;

echo "<pre>";
var_dump($arrNxm);
echo "</pre>";

You've got some logical issues in your code which is causing it to not work.您的代码中有一些逻辑问题导致它无法工作。 You are going along the right lines though.不过,您正在沿着正确的路线前进。

Issue 1第一期

You have set the number of rows and columns incorrectly.您错误地设置了行数和列数。 There are not 4, there are 5 of each:没有 4 个,每个有 5 个:

$intRows = 5;
$intCols = 5;

Issue 2第 2 期

The next issue is with:下一个问题是:

$minIndex = $maxIndex = 1;

If $minIndex is 1 , then nothing in your array can be lower than it, so it's never going to be updated from 1. This should be something like:如果$minIndex1 ,那么你的数组中没有任何东西可以低于它,所以它永远不会从 1 更新。这应该是这样的:

$minIndex = 100000; // This is an arbitrary choice to fix the issue
$maxIndex = 1;

Issue 3第 3 期

Next, you use $arrNxm in your code.接下来,您在代码中使用$arrNxm There is no $arrNxm , it should be $array .没有$arrNxm ,应该是$array

Issue 4第 4 期

Finally, this is wrong because you are putting the minimum value back into the minimum position in the array:最后,这是错误的,因为您将最小值放回数组中的最小值 position 中:

$arrNxm[$intMinRow][$intMinCol] = $minIndex;
$arrNxm[$intMaxRow][$intMaxCol] = $maxIndex;

You just need to swap $minIndex and $maxIndex around:你只需要交换$minIndex$maxIndex

$array[$intMinRow][$intMinCol] = $maxIndex;
$array[$intMaxRow][$intMaxCol] = $minIndex;

Full code完整代码

The fully working code is:完整的工作代码是:

$array = [
  [45, 456, 321, 344, 567],
  [100, 434, 173, 400, 789],
  [191, 211, 457, 809, 900],
  [431, 323, 432, 805, 906],
  [708, 232, 897, 101, 696]
];

$intRows = 5;
$intCols = 5;

$intMaxRow = $intMinRow = $intMaxCol = $intMinCol = 0;

$minIndex = 100000;
$maxIndex = 1;

for($row = 0; $row < $intRows; $row++)
{
  for($col = 0; $col < $intCols; $col++)
  {
    if($array[$row][$col] > $maxIndex)
    {
      $maxIndex = $array[$row][$col];
      $intMaxRow = $row;
      $intMaxCol = $col;
    }
    if($array[$row][$col] < $minIndex)
    {
      $minIndex = $array[$row][$col];
      $intMinRow = $row;
      $intMinCol = $col;
    }
  }
}

$array[$intMinRow][$intMinCol] = $maxIndex;
$array[$intMaxRow][$intMaxCol] = $minIndex;

echo "<pre>";
var_dump($array);
echo "</pre>";

This outputs:这输出:

array(5) {
  [0]=>
  array(5) {
    [0]=>
    int(906)
    [1]=>
    int(456)
    [2]=>
    int(321)
    [3]=>
    int(344)
    [4]=>
    int(567)
  }
  [1]=>
  array(5) {
    [0]=>
    int(100)
    [1]=>
    int(434)
    [2]=>
    int(173)
    [3]=>
    int(400)
    [4]=>
    int(789)
  }
  [2]=>
  array(5) {
    [0]=>
    int(191)
    [1]=>
    int(211)
    [2]=>
    int(457)
    [3]=>
    int(809)
    [4]=>
    int(900)
  }
  [3]=>
  array(5) {
    [0]=>
    int(431)
    [1]=>
    int(323)
    [2]=>
    int(432)
    [3]=>
    int(805)
    [4]=>
    int(45)
  }
  [4]=>
  array(5) {
    [0]=>
    int(708)
    [1]=>
    int(232)
    [2]=>
    int(897)
    [3]=>
    int(101)
    [4]=>
    int(696)
  }
}

Whereby 45 and 906 are swapped in position as you wanted.从而根据需要在 position 中交换 45 和 906。

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

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