在 PHP 中对多维数字数组进行排序

发布于 2024-11-14 10:24:13 字数 593 浏览 0 评论 0原文

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

数组看起来像:

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

现在我想按“列”对它们进行排序。这意味着,第一列数字 (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");

我该怎么做?谢谢

I have issues sorting an multidimensional array.

The array looks like:

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

Now I want to sort them by "column". That means, the first column of numbers (100, 293, 842) must be sorted, then the second column (but keeping the first column as it is! It may happen that the columns have the same number with multiple rows) and so on.

Actually I tried this to do with usort(), but this will work only when sorting the first column:

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

How can I do this? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无畏 2024-11-21 10:24:13

这是可能的解决方案:

  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    

Here's possible solution:

  1. get rid of 'token', i.e., make 2D array
  2. swap columns and rows (transpose array)
  3. sort each column
  4. swap back columns and rows (get initial structure)
  5. put 'token' back

Code:

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

Output:

100    23     72     2      17     
293    28     283    23     28     
842    240    348    98     114    
半枫 2024-11-21 10:24:13

我想这会做你所追求的。我在这里做了一些假设(例如$array确实是一个数组,至少有一个子数组,所有子数组都有token code> 作为键,并且所有子数组具有相同数量的元素)。

<?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]==>整数(17) } }
数组(1){ [“令牌”] =>数组(5){
[0] => int(293)[1]==> int(28) [2]==>
int(283) [3]==> int(23) [4]==>整数(28) }
} array(1) { ["token"]=>;数组(5){
[0] => int(842)[1]==> int(240) [2]==>
int(348) [3]==> int(98)[4]==>整数(114)
} }

I think this will do what you are after. I've made some assumptions here (such as $array is really an array, has at least one sub-array, all sub-arrays have token as the key, and all sub-arrays have the same number of elements).

<?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>';
}

Output:

array(1) { ["token"]=> array(5) {
[0]=> int(100) [1]=> int(23) [2]=>
int(72) [3]=> int(2) [4]=> int(17) } }
array(1) { ["token"]=> array(5) {
[0]=> int(293) [1]=> int(28) [2]=>
int(283) [3]=> int(23) [4]=> int(28) }
} array(1) { ["token"]=> array(5) {
[0]=> int(842) [1]=> int(240) [2]=>
int(348) [3]=> int(98) [4]=> int(114)
} }

智商已欠费 2024-11-21 10:24:13

你不能只是

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

或者我误解了这个问题吗?

Can't you just

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

Or have I misunderstood the question?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文