-1

我正在尝试按特定顺序对数组进行排序:

我的代码(当前数组):

Array
(
[25] => Array
    (
        [1st place] => 
    )

[15] => Array
    (
        [2nd place] => 
    )

[10] => Array
    (
        [3rd place] => 
    )

[5] => Array
    (
        [4th place] => 
        [5th place] => 
        [6th place] => 
        [7th place] => 
        [8th place] => 
        [9th place] => 
        [10th place] => 
    )

[1] => Array
    (
        [11th place] => 
        [12th place] => 
        [13th place] => 
        [14th place] => 
        [15th place] => 
        [16th place] => 
        [17th place] => 
        [18th place] => 
        [19th place] => 
        [20th place] => 
        [21st place] => 
        [22nd place] => 
        [23rd place] => 
        [24th place] => 
        [25th place] => 
    )

)

需要数组:

Array
(
[0] => Array
    (
        [1st place] => 25
    )

[1] => Array
    (
        [2nd place] => 15
    )

[5] => Array
    (
        [3rd place] => 10
    )

[3] => Array
    (
        [4th place] => 5
        [5th place] => 5
        [6th place] => 5
        [7th place] => 5
        [8th place] => 5
        [9th place] => 5
        [10th place] => 5
    )

[4] => Array
    (
        [11th place] => 1
        [12th place] => 1
        [13th place] => 1
        [14th place] => 1
        [15th place] => 1
        [16th place] => 1
        [17th place] => 1
        [18th place] => 1
        [19th place] => 1
        [20th place] => 1
        [21st place] => 1
        [22nd place] => 1
        [23rd place] => 1
        [24th place] => 1
        [25th place] => 1
    )

)

这个想法就像使用数组位置(?)作为键的值并按以下顺序设置数组:

我正在尝试使用该代码(php)

foreach ($newOptions as $ord) {
            $place = $ord[$idx];
            $value = $ord[$idx][0];
            $newOrderarr[$idx][$value][$place] = $name2;
            $idx++;
        }

但也不是很好 $newoptions 是我使用的数组...

4

5 回答 5

5

没有人采用明显的排序方法吗?

uasort($data, function($a, $b) {
    reset($a);
    reset($b);
    $aVal = (int) key($a);
    $bVal = (int) key($b);

    if ($aVal < $bVal) {
        return -1;
    } elseif ($bVal < $aVal) {
        return 1;
    } else {
        return 0;
    }
});

请注意,这会重置子数组中的内部指针,但这不太可能成为问题,并且它们可能是副本。此外,如果您在那里有一个空的子数组,它将无法工作。在这种情况下,我只需array_filter先调用它来删除空值。

于 2012-11-20T18:36:59.113 回答
2

你可以试试这个:

foreach($your_array as $key => &$bla) {
    foreach($bla as &$item) {
        $item = $key;
    }
}

sort($your_array);

但我宁愿从一开始就修复数据结构,这似乎至少......很奇怪。

于 2012-11-20T18:57:35.693 回答
1

我真的不明白你最终是如何得到你的数组结构的,但如果有充分的理由这样做,你可以像这样进行排序:

#recreating your data:
$newOptions = array();
$newOptions[25] = array('1st place' => '');
$newOptions[15] = array('2nd place' => '');
$newOptions[10] = array('3rd place' => '');
$newOptions[5] = array('4th place' => '', '5th place' => '', '6th place' => '', '7th place' => '', '8th place' => '', '9th place' => '', '10th place' => '');
$newOptions[1] = array('11th place' => '', '12th place' => '', '13th place' => '', '14thth place' => '', '15thth place' => '', '16th place' => '', '...' => '');

#sorting as you wanted
$newArray = array();
foreach ($newOptions as $id => $options) {
   $newOptions = $options;
   foreach ($newOptions as $optid => $option) {
      $newOptions[$optid] = $id;
   }
   $newArray[] = $newOptions;
}
$newOptions = $newArray;
于 2012-11-20T16:28:42.030 回答
1

你的结构对我没有意义?我选择了稍微不同的东西,但希望能很好地满足您的需求!

<?php
$scores = array(15, 20, 50, 100, 4);
sort($scores);

for($i = 0; $i < count($scores); $i++) {
    $new_array[] = array('score' => $scores[$i], 'text' => ordinal($i + 1) . " Place");
}

print_r($new_array);




// Ordinal from PHP docs
function ordinal($i = '') {
  $o=$i;
  $s=array('th', 'st', 'nd', 'rd');

  if(!is_int($o))
    if(ctype_digit($o))
      $o=(int)substr($o,-2,2);
    else
      return(false);
  return($i.$s[($o%100>10&&$o%100<20)?0:($o%10<4?$o%10:0)]);
}

这会产生输出

Array
(
    [0] => Array
        (
            [score] => 4
            [text] => 1st Place
        )

    [1] => Array
        (
            [score] => 15
            [text] => 2nd Place
        )

    [2] => Array
        (
            [score] => 20
            [text] => 3rd Place
        )

    [3] => Array
        (
            [score] => 50
            [text] => 4th Place
        )

    [4] => Array
        (
            [score] => 100
            [text] => 5th Place
        )

)
于 2012-11-20T16:29:57.520 回答
1
$newOrderarr = array();
$i = 0;
foreach ($newOptions as $k => $v){
    foreach ($v as $k2 => $v2){
        $newOrderarr[$i][$k2] = $k;
    }
    $i++;
}

不过没时间测试。

于 2012-11-20T16:35:38.980 回答