Php 第n项上的数组排序
假设我有一个数组Php 第n项上的数组排序,php,Php,假设我有一个数组 array('one','two','three','four','five','six','seven','eight','nine','ten'); 我想每订购三件商品,这样现在我就可以: array('one','five','nine','two','six','ten','three','seven','x','four','eight','x'); 基本上,最终目标是让花车从上到下排列,而不是从左到右排列。我知道其他方法(要么放弃IE9-,用SCS胡闹,要么使用
array('one','two','three','four','five','six','seven','eight','nine','ten');
我想每订购三件商品,这样现在我就可以:
array('one','five','nine','two','six','ten','three','seven','x','four','eight','x');
基本上,最终目标是让花车从上到下排列,而不是从左到右排列。我知道其他方法(要么放弃IE9-,用SCS胡闹,要么使用javascript)。除了失去旧的IE9支持之外,使用PHP是对资源影响最小的,所以让我们只关注PHP
现在,我可以看到我需要将原始数组填充为3的倍数,但是如果没有三个嵌套循环和模表达式,我想不出获得所需顺序的方法
数组值是占位符,所以不要挂断这些占位符。$a=array('1','2','3','4','5','6','7','8','9','10');
$a = array('one','two','three','four','five','six','seven','eight','nine','ten');
// prepare
$n = 0; // counter
$x = array(); // storage
$f = ceil(count($a)/3); // we need a number of arrays so that each
// one has to hold no more than three entries
// distribute
foreach ($a as $entry) {
$i = $n++ % $f; // calculate into which array to drop the entry
$x[$i][] = $entry; // drop it
}
// collect it
$z = array();
for ($i = 0; $i<$f; $i++) {
if (count($x[$i])<count($x[0])) $x[$i][] = 'x'; // pad it if too short
$z = array_merge($z, $x[$i]);
}
//预备
$n=0;//柜台
$x=数组();//存储
$f=ceil(计数($a)/3);//我们需要多个阵列,以便每个阵列
//一个人必须持有不超过三个条目
//分发
foreach($a作为$entry){
$i=$n++%$f;//计算要将条目放入哪个数组
$x[$i][]=$entry;//删除它
}
//收集
$z=数组();
对于($i=0;$i您不需要对数组进行排序。我有时会这样解决:
$col = 0;
$cols = 3;
foreach($array as $number){
$col++;
if($col == 1){
echo '<tr>'
}
echo '<td>'.$number.'</td>';
if($col == $cols){
echo '</tr>'
$col = 0;
}
}
$col=0;
$cols=3;
foreach($array作为$number){
$col++;
如果($col==1){
回声“
}
回显“.$number.”;
如果($col==$cols){
回声“
$col=0;
}
}
高级:您只需要一个循环,就可以在cols amd行之间输出所有您喜欢的代码。这里有一个解决方案,输出在
$output
中
// Input
$data = array('one','two','three','four','five','six','seven','eight','nine','ten');
$groups = 3;
// Calculations
$num_per_group = ceil(count($data) / $groups);
$final_size = $groups * $num_per_group; // Multiple of $groups
$data = array_pad($data, $final_size, "x"); // Pad to $final_size
$output = array();
for ($i = 0; $i < $num_per_group; $i++) { // Outer loop, by # per group
for ($j = 0; $j < $groups; $j++) { // Inner loop, by # of groups
$output[] = $data[$j * $num_per_group + $i];
}
}
一个更精细的解决方案,只需在一个整洁的函数中使用一个循环:
function pad_and_transpose_columns($data, $groups) {
$num_per_group = ceil(count($data) / $groups);
$final_size = $groups * $num_per_group; // Multiple of $groups
$data = array_pad($data, $final_size, "x"); // Pad to $final_size
$output = array();
for ($i = 0; $i < $final_size; $i++) {
$output[] = $data[($i * $num_per_group + floor($i / $groups)) % 12];
}
return $output;
}
$data = array('one','two','three','four','five','six','seven','eight','nine','ten');
$output = pad_and_transpose_columns($data, 3);
// array('one','five','nine','two','six','ten','three','seven','x','four','eight','x');
功能垫和转置列($data$groups){
$num_per_group=ceil(计数($data)/$groups);
$final\u size=$groups*$num\u per\u组;//多个$groups
$data=array_pad($data,$final_size,“x”);//pad到$final_size
$output=array();
对于($i=0;$i<$final_size;$i++){
$output[]=$data[($i*$num_/组+楼层($i/$groups))%12];
}
返回$output;
}
$data=数组('1','2','3','4','5','6','7','8','9','10');
$output=pad_和_transpose_列($data,3);
//数组('1','5','9','2','6','10','3','7','x','4','8','x');
我读了5遍,仍然看不清你想做什么…@Abracadver他的措辞很糟糕…我想他的意思是他想把原始数组分成3组,然后转置/压缩它们并展平数组,而不是“每三项订购”抱歉,但我想让数组1弹出,因为数组2就是全部。
function pad_and_transpose_columns($data, $groups) {
$num_per_group = ceil(count($data) / $groups);
$final_size = $groups * $num_per_group; // Multiple of $groups
$data = array_pad($data, $final_size, "x"); // Pad to $final_size
$output = array();
for ($i = 0; $i < $final_size; $i++) {
$output[] = $data[($i * $num_per_group + floor($i / $groups)) % 12];
}
return $output;
}
$data = array('one','two','three','four','five','six','seven','eight','nine','ten');
$output = pad_and_transpose_columns($data, 3);
// array('one','five','nine','two','six','ten','three','seven','x','four','eight','x');