Php 升序排序,从第二个或第三个ID开始并返回..因为缺少更好的描述
这可能是一个简单的问题,但我不能把我的头绕在它周围…我研究的所有内容都提到了数组的升序或降序排序。。。 我有一个数组['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 升序排序,从第二个或第三个ID开始并返回..因为缺少更好的描述,php,arrays,sorting,Php,Arrays,Sorting,这可能是一个简单的问题,但我不能把我的头绕在它周围…我研究的所有内容都提到了数组的升序或降序排序。。。 我有一个数组['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级别
这很明显吗?我仍然处于PHP级别的最低级别:)您可以实现一些自定义迭代器。比如:
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;
}
}
}
将输出
2 => Carl
3 => Doris
4 => Erik
0 => Alphred
1 => Bob
另一种方法是使用
array\u merge
和array\u 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));
$myArray=array(4,5,6,7,8,1,2,3);
函数customSort($array,$start=7)
{
排序($数组);
返回数组合并(数组切片($array,$start-1),数组切片($array,0,$start-1));
}
回声';
var_dump(customSort($myArray));
我认为描述不是很清楚,在某种程度上它给人的印象是关于排列的。。无论如何,如果我理解正确,并且您的数字是连续的,这可能会有所帮助:
我也喜欢blausocke的答案 谢谢大家,你们给我指出了一些好的方向。 我发现了一个非常简单的解决方案,它适合我,并且是可扩展的: 我的$firstarray在上升1,2,3,4,这一个保持不变 在第二个阵列上: 我使用了array\u shift($myarray);要从数组中删除第一个值并将其存储为$first_值, 然后我用 数组\u推送($myarray,$first\u值);在数组末尾插入第一个值。 $myarray now=2,3,4,1=$secondarray 对于$thirdarray,我使用$secondarray并对其执行与第二个相同的操作。 所以$thirdarray=3,4,1,2 它有点粗糙,但适合我的目的,只有6行左右!
谢谢大家……你们可能想用usort()看这里:天哪,这比我想象的要复杂得多!我会尽力弄明白这一切,然后回到这里。@user1924460,但可重用,可插拔。:)这并不像你想象的那么复杂。。。这是一个简单的迭代器。它看起来是这样的,因为我选择实现它以使其与常规迭代器兼容。最复杂的方法是
valid
,它只是一个简单的算法,$count
保存我们调用next
的次数。如果将$start
设置为一个较小的值(如1
),它就不能按预期工作。这基本上与我提出的想法相同。
$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));
function test($min, $max, $start){
if( $start === $min)
return range($min,$max);
return array_merge(range($start,$max), range($min,$start-1));
}