dongqiya9552 2013-12-16 07:43
浏览 27
已采纳

批量大批量列入较小批次

i have a list like-

array(
[0] => (string)"A.1.failure",
[1] => (string)"A.2.failure",
[2] => (string)"A.3.failure",
[3] => (string)"A.4.failure",
[4] => (string)"B.1.failure",
[5] => (string)"B.1.failure",
.
.
)

And i want to make curl calls in batches, of say- 4. In that case i want something like-

[0] => (string)
         "&target=A.1.failure
          &target=A.2.failure
          &target=A.3.failure
          &target=B.1.failure",

[1] => (string)
         "&target=B.2.failure
          &target=B.3.failure
          &target=B.4.failure
          &target=C.1.failure",

Is there an in-built/commonly used method in PHP that can simplify this? The C like code that i have written gets too complex to debug-

private function _aggregate_metric_list_into_batches($metric_list)
{
$batch_calls     = NULL;
$batched_list    = array();
$calls_per_batch = $this->_curl_call_batch_size;

if ($this->_flag_skip_inactive_instances)
{
    $running_instances = $this->_get_ip_of_running_instances(INSTANCE_ROLE_SPIDERMAN);
}

$num_of_batches = ceil(count($metric_list) / $calls_per_batch);
var_dump($num_of_batches);
for ($current_batch = 0; $current_batch < $num_of_batches; $current_batch++)
{
    $batched_list[$current_batch] = array();

    // $batch_offset < count($metric_list)-1 so that $j doesn't roll upto $calls_per_batch at the last iteration.
    $batch_offset = 0;
    for ($j = 0; $j < $calls_per_batch && $batch_offset < count($metric_list) - 1; $j++)
    {
        $batch_offset = (($current_batch * $calls_per_batch) + $j);
        $core_metric  = $metric_list[$batch_offset];
        $exploded     = explode(".", $core_metric);
        $metric_ip    = $exploded[4];

        if ($this->_flag_skip_inactive_instances AND !in_array($metric_ip, $running_instances))
        {
            // If --skip-inactive-instances flag is not NULL, skip those which aren't running.
            if($this->_flag_verbosity)
            {
                Helper_Common::log_for_console_and_human("Ignoring $metric_ip.", LOG_TYPE_INFO);
            }
            continue;
        }
        if ($this->_apply_metric_function === WY_FALSE)
        {
            $prepared_metric = "&target=".$core_metric;
        }
        else
        {
            $prepared_metric = "&target=".$this->_metric_function."(".$core_metric.",".$this->_metric_function_arg.")";
        }
        $batch_calls     = $batch_calls.$prepared_metric;
    }

    $batched_list[$current_batch] = $batch_calls;
    $batch_calls          = "";
}
  • 写回答

1条回答 默认 最新

  • dongsheng4126335 2013-12-16 07:48
    关注

    Use array_chunk function Example:

       $input_array = array('A.1.failure', 'A.2.failure', 'A.3.failure', 'B.1.failure', 'B.2.failure', 'B.3.failure', 'B.4.failure');
    print_r(array_chunk($input_array, 4));
    

    It will chunk it into for elements in each array

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算