数组顺序 - 数组

时间:2012-11-20 16:16:00

标签: php arrays

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

我的代码(当前数组):

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是我正在使用的数组......

5 个答案:

答案 0 :(得分: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就可以删除空值。

答案 1 :(得分:2)

你可以试试这个:

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

sort($your_array);

但我宁愿修改数据结构,看起来至少......很奇怪。

答案 2 :(得分: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;

答案 3 :(得分: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
        )

)

答案 4 :(得分:1)

$newOrderarr = array();
$i = 0;
foreach ($newOptions as $k => $v){
    foreach ($v as $k2 => $v2){
        $newOrderarr[$i][$k2] = $k;
    }
    $i++;
}

没有时间测试它。