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

时间:2019-10-01 11:20:09

标签: php arrays

我正在编写一个程序,该程序在多维数组中交换最大和最小的数字。最大的数字应该放在最小的地方,最小的地方应该是最大的地方,其想法是使用纯逻辑,而没有任何可以帮助我的php函数。

请检查我的代码,并帮助我解决这个问题。

例如:

$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]
];

新订单应为:

$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]
]

我已经尝试过用这段代码添加和更改代码,但这并不能给我正确的结果...

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

1 个答案:

答案 0 :(得分:2)

您的代码中存在一些逻辑问题,导致其无法正常工作。不过,您的做法正确。

问题1

您错误地设置了行数和列数。没有4,每个有5:

$intRows = 5;
$intCols = 5;

问题2

下一个问题是:

$minIndex = $maxIndex = 1;

如果$minIndex1,则数组中的任何内容都不能低于它,因此它永远不会从1更新。这应该类似于:

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

问题3

接下来,您在代码中使用$arrNxm。没有$arrNxm,应该是$array

问题4

最后,这是错误的,因为您将最小值放回了到数组中的最小位置:

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

您只需要交换$minIndex$maxIndex

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

完整代码

完整的代码是:

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

这将输出:

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)
  }
}

根据需要将45和906交换位置。