0

这可能是一个简单的问题,但我无法理解它......我研究的所有内容都提到了数组的升序或降序排序......我有一个数组['id','things']。

Foreach ($things as $thing) 循环按以下顺序回显它:1,2,3,4(显而易见)我如何对它进行排序:2,3,4,1 然后是 3,4,1,2 和也许是 4,1,2,3 ... 只是按升序排列,但每次都从不同的 Id 开始... 而表中的实际 ID 不一定是 1,2,3,4 可以是 5,6 ,7,8(尽管总是在一个组中)

这很明显吗?我仍然处于 PHP 规模的最低级别 :)

4

4 回答 4

1

您可以实现一些自定义迭代器。就像是 :

class ArrayOffsetIterator implements Iterator {
    private $_array;
    private $_index;
    private $_startIndex;

    public function __construct(array $arr, $startIndex = 0) {
        if ($startIndex < 0) throw new Exception("Start index must be greater than zero");

        $this->_array = (array) $arr;

        if (empty($this->_array)) {
            $this->_index = $this->_startIndex = 0;
        } else {
            $this->_index = $this->_startIndex = $startIndex % count($arr);
        }
    }

    public function current() {
        if (empty($this->_array)) {
            return false;
        } else {
            return $this->_array[$this->key()];
        }
    }

    public function key() {
        if (empty($this->_array)) {
            return false;
        } else {
            return $this->_index % count($this->_array);
        }
    }

    public function next() {
        if (!$this->valid()) throw new Exception("End of iterator");
        $this->_index++;
        return $this->current();
    }

    public function rewind() {
        $this->_index = $this->_startIndex;
    }

    public function valid() {
        $size = count($this->_array);
        if ($size == 0) {
            return false;
        } else {
            $count = ($this->_index % $size) + (((int) ($this->_index / $size)) * $size) - $this->_startIndex;
            return $count < $size;
        }
    }
}

并以这种方式使用它:

// some unsorted array
$array = array(
    'Erik',
    'Carl',
    'Alphred',
    'Doris',
    'Bob',
);

// sort it however you like...
sort($array);

// create your iterator (we'll start at index 2 of 0 based index)
$iterator = new ArrayOffsetIterator($array, 2);

// because it's an iterator, we can use it in foreach directly
foreach ($iterator as $key => $item) {
   echo $key . ' => ' . $item . "\n";
}

将输出

2 => Carl
3 => Doris
4 => Erik
0 => Alphred
1 => Bob
于 2013-01-07T00:23:10.763 回答
0

谢谢大家,你们都为我指明了一些好的方向。我找到了一个对我有用且可扩展的非常简单的解决方案:我的 $firstarray 上升了 1,2,3,4,这个保持不变。

在第二个数组上:我使用了 array_shift($myarray); 从数组中删除第一个值并将其存储为 $first_value,然后我使用 array_push($myarray, $first_value); 在数组末尾插入第一个值。$myarray 现在 = 2,3,4,1 = $secondarray

对于 $thirdarray,我使用了 $secondarray 并对其进行了与第二个相同的操作。所以 $thirdarray = 3,4,1,2

这有点粗糙,但适合我的目的,只有 6 行左右!谢谢你们....

于 2013-01-07T06:17:15.097 回答
0

另一种方法是使用array_mergeand array_slice,如下所示:

$myArray = array(4, 5, 6, 7, 8, 1, 2, 3);

function customSort ($array, $start = 7)
{
    sort($array);
    return array_merge(array_slice($array, $start - 1), array_slice($array, 0, $start - 1));
}

echo '<pre>';
var_dump(customSort($myArray));
于 2013-01-07T00:31:46.310 回答
0

我认为描述不是那么清楚,在某些时候它给人的印象是它是关于排列的。无论如何,如果我理解正确并且你的数字是连续的,也许这可能会有所帮助:

function test($min, $max, $start){
    if( $start === $min)
        return range($min,$max);
    return array_merge(range($start,$max), range($min,$start-1));
}

我也喜欢 blausocke 的回答。

于 2013-01-07T01:28:04.803 回答