doujiao1984 2014-12-29 22:02
浏览 29

如何使用可能的空插槽扩展阵列?

I'm working with multi-dimesional arrays and am stumped trying to create a dynamic compatibility chart.

Given Data : 
4 instances of Value 1
7 instances of Value 2

Ideal End Result Examples

If there are 4 value 1's and 7 value 2's

[0] Value 2
[1] Value 1
[2] Value 2
[3] 
[4] Value 2
[5] Value 1
[6] Value 2
[7] 
[8] Value 2
[9] Value 1
[10] Value 2
[11] Value 1
[12] Value 2

If there are 4 value 1's and 4 value 2's (try to evenly space them out without overlapping)

[0] Value 1
[1] Value 2
[2]
[3] Value 1
[4] Value 2
[5]
[6] 
[7] Value 1
[8] Value 2
[9] 
[10] 
[11] Value 1
[12] Value 2

If there are 2 instances of 1 and three of 2.

[0] Value 1
[1] Value 2
[2]
[3] 
[4] 
[5]
[6] Value 2
[7] 
[8] 
[9] 
[10] 
[11] Value 1
[12] Value 2

I have bucket sizes of 12-24 for an array. In the example I use 12. If number of instances don't fit in 12 buckets, can move up all the way to 24. If it doesn't fit in 24, give an error.

Any loop that I've tried to make by using array_pop and reversing arrays creates empty holes in the middle of the array or it doesn't spread out the instances evenly.


Edit: Here is what I've tried.

$table_array = range(0,12);
// Method 1 

for ($i = 0; sizeof($table_array); $i++)
{
    $ready_array[] = ($i % 2) ? array_pop($table_array) : array_shift($table_array);
}


// Method 2

for ($i = 0; $i < sizeof($table_array); $i++)
{
    $index = ($i % 2) ? sizeof($table_array) - ceil($i / 2) : ceil($i / 2);
    $ready_array[$index] = $table_array[$i];
}
ksort($ready_array);

Project Details and Goal

I have a list of compatible and incompatible pills.

I let the users select which pills and how many they're taking.

Based on that, I search the database and match up pills based on compatibility creating a lists known as Value 1 and Value 2.

I have a minimum 12 hour time frame to take the pills.

I'm supposed to space out lets say 4 of Calcium and 3 of Iron evenly across 12 hour period with 1 hour intervals and not let them overlap. If they choose 12 pills of Calcium and 12 pills of Iron, I can push up to 24 hours to make them take those pills.

  • 写回答

1条回答 默认 最新

  • doupu3635 2014-12-29 23:38
    关注

    Some ideas for your question : Use array_chunk() and array_count_values()

    <?php 
    
    $myarray = array(2,1,2,'',2,1,2,'',2,1,2,1,1,2,1,2,'',2,1,2,'',2,1,2,1,2);
    $tests = array_chunk($myarray, 13);
    //print_r($tests);
    
    function mytest($tests)
    {
        foreach($tests as $test) 
        {
            // play your test !
            print_r(array_count_values($test));
        }    
    }
    // add your argument for your test
    echo mytest($tests);
    ?>
    

    output :

    Array
    (
        [2] => 6
        [1] => 5
        [] => 2
    )
    Array
    (
        [2] => 7
        [1] => 4
        [] => 2
    )
    

    See for test : http://codepad.org/M9u5G0je

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了