填充4阵列均匀地分布尽可能从上到下 [英] Populate 4 arrays evenly distributed as much as possible from top to bottom

查看:112
本文介绍了填充4阵列均匀地分布尽可能从上到下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于这个职位
<一href=\"http://stackoverflow.com/questions/18051240/how-to-distribute-mysql-result-set-in-an-multidimensional-array-of-4-arrays\">How分发的MySQL结果在4数组的多维数组设置

我接受的答案,但现在我想做出改变到code和我不是有很多成功的...

基本上,从MySQL结果集,我需要填充4阵列分布均匀,尽可能上下...
克里斯·海耶斯提供了一个可行的solutuon,但是当我今天测试了它,我意识到它填充从左分辩的阵列,而不是从上到下...

我要如何改变code所以它填充4阵列尽可能从上到下?

  $ I = 0;
$ array_r =阵列(阵列(),()数组,数组(),阵列());而($ stmt-&GT;取()){
    array_push($ array_r [$ i],阵列(... ...值));
    $ I =($ I + 1)%4;
}


解决方案

最终版本没有操纵输入数组在所有的:

 为($ NUM =计数($输入),$抵消= 0; $ numBuckets大于0; $ numBuckets  -  = 1,$ NUM  -  = $ bucketSize,$胶印+ = $ bucketSize){
  $ bucketSize = CEIL($ NUM / $ numBuckets);
  $输出[] = array_slice($投入,$抵消,$ bucketSize);
}


透水答案的:

请尝试以下操作:

 &LT; PHP
$输入=范围('A','Z'); //测试输入数据
$输出=阵列(); //输出容器
$ numBuckets = 4; //桶数量,以填补为(; $ numBuckets大于0; $ numBuckets - = 1){
  $输出[] = array_splice($输入,0,CEIL(计数($输入)/ $ numBuckets));
}的print_r($输出);


另一个版本,没有持续的复查的数组的长度

 为($ NUM =计数($输入); $ numBuckets大于0; $ numBuckets  -  = 1,$ NUM  -  = $ bucketSize){
  $ bucketSize = CEIL($ NUM / $ numBuckets);
  $输出[] = array_splice($输入,0,$ bucketSize);
}

This question is in relation to this post How to distribute mysql result set in an multidimensional array of 4 arrays

I got the accepted answer but now i want to make a change to the code and i'm not having a lot of success...

Basically, from a mysql result set, i need to populate 4 arrays evenly distributed as much as possible from top to bottom... Chris Hayes provided a solutuon that works, but when i tested it today, i realize that it populates the array from left to rigth, and not from top to bottom...

How do i change the code so it populates the 4 arrays as much as possible from top to bottom ?

$i = 0;
$array_r = array( array(), array(), array(), array() );

while ($stmt->fetch()) {
    array_push($array_r[$i], array(... values ...));
    $i = ($i + 1) % 4;
}

解决方案

final version without manipulating the input array at all:

for ($num = count($input), $offset = 0; $numBuckets > 0; $numBuckets -= 1, $num -= $bucketSize, $offset += $bucketSize) {
  $bucketSize = ceil($num / $numBuckets);
  $output[] = array_slice($input, $offset, $bucketSize);
}


pervious answer:

Try the following:

<?php
$input = range('A', 'Z'); // test input data
$output = array();        // the output container
$numBuckets = 4;          // number of buckets to fill

for (; $numBuckets > 0; $numBuckets -= 1) {
  $output[] = array_splice($input, 0, ceil(count($input) / $numBuckets));
}

print_r($output);


alternative version, without constant rechecking the length of the array

for ($num = count($input); $numBuckets > 0; $numBuckets -= 1, $num -= $bucketSize) {
  $bucketSize = ceil($num / $numBuckets);
  $output[] = array_splice($input, 0, $bucketSize);
}

这篇关于填充4阵列均匀地分布尽可能从上到下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆