【问题标题】:Sorting multidimensional number array in PHP在PHP中对多维数字数组进行排序
【发布时间】:2011-06-08 09:22:43
【问题描述】:

我在排序多维数组时遇到问题。

数组看起来像:

$数组 = 数组( 数组(“令牌” => 数组(100、240、348、23、17), 数组(“令牌” => 数组(293、28、283、2、28), 数组(“令牌”=> 数组(842、23、72、98、114) );

现在我想按“列”对它们进行排序。这意味着,必须对第一列数字(100、293、842)进行排序,然后是第二列(但保持第一列不变!可能会发生列具有相同数字且多行)等等.

其实我试过用 usort() 来做这个,但这仅在对第一列进行排序时才有效:

function do_sort($a, $b) {
  $tok_a = $a["token"];
  $tok_b = $b["token"];

  if ($tok_a[0] <= $tok_b[0])
    return false;
  else
    return true;
}

usort($array, "do_sort");

我该怎么做?谢谢

【问题讨论】:

标签: php sorting numbers


【解决方案1】:

以下是可能的解决方案:

  1. 摆脱“令牌”,即制作二维数组
  2. 交换列和行(转置数组)
  3. 对每一列进行排序
  4. 交换回列和行(获取初始结构)
  5. 放回“令牌”

代码:

function array_transpose(array $array) {
    $result = array();
    foreach ( $array as $rowNum => $row ) {
        foreach ( $row as $colNum => $value ) {
            $result[$colNum][$rowNum] = $value;
        }
    }
    return $result;
}

$array = array(
  array("token" => array(100, 240, 348, 23, 17)),
  array("token" => array(293, 28, 283, 2, 28)),
  array("token" => array(842, 23, 72, 98, 114)),
);

// get rid of 'token'
foreach ( $array as &$item ) {
    $item = $item['token'];
}
unset($item);

// swap columns and rows
$array = array_transpose($array);

// sort columns
foreach ( $array as &$item ) {
    sort($item);
}
unset($item);

// swap back columns and rows
$array = array_transpose($array);

// put 'token' back
foreach ( $array as &$item ) {
    $item = array('token' => $item);
}
unset($item);

// display results
foreach ( $array as $row ) {
    foreach ( $row['token'] as $value ) {
        printf('%-7d', $value);
    }
    echo "\n";
}

输出:

100    23     72     2      17     
293    28     283    23     28     
842    240    348    98     114    

【讨论】:

    【解决方案2】:

    我认为这将满足您的需求。我在这里做了一些假设(比如$array真的一个数组,至少有一个子数组,所有子数组都以token为key,所有子数组具有相同数量的元素)。

    <?php
    
    $array = array(
      array("token" => array(100, 240, 348, 23, 17)),
      array("token" => array(293, 28, 283, 2, 28)),
      array("token" => array(842, 23, 72, 98, 114)),
    );
    
    $count_outer = count($array);
    $count_inner = count($array[0]['token']);
    
    for ($i=0; $i < $count_inner; $i++) {
        $temp_arr = array();
    
        for ($j=0; $j < $count_outer; $j++) {
            $temp_arr[] = $array[$j]['token'][$i];
        }
        sort($temp_arr);
    
        for ($j=0; $j < $count_outer; $j++) {
            $array[$j]['token'][$i] = $temp_arr[$j];
        }
    }
    
    foreach ($array as $value) {
        var_dump($value);
        echo '<br>';
    }
    

    输出:

    数组(1){[“令牌”]=>数组(5){ [0]=> int(100) [1]=> int(23) [2]=> int(72) [3]=> int(2) [4]=> int(17) } } 数组(1){[“令牌”]=>数组(5){ [0]=> int(293) [1]=> int(28) [2]=> int(283) [3]=> int(23) [4]=> int(28) } } 数组(1){ [“令牌”]=> 数组(5){ [0]=> int(842) [1]=> int(240) [2]=> 整数(348) [3]=> 整数(98) [4]=> 整数(114) } }

    【讨论】:

    • 好消息是我们以同样的方式理解作者并且我们得到了同样的结果(虽然解决方案不同)。
    【解决方案3】:

    你就不能

    foreach ($array as &$item) {
      sort($item['token']);
    }
    

    还是我误解了这个问题?

    【讨论】:

    • 我认为您误解了这个问题。如果我们将每个$item['token'] 视为“行”,则“列”需要排序,而不是“行”。至少我和@Aether 是这样理解这个问题的。
    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多