批量大批量列入较小批次

时间:2013-12-16 07:43:14

标签: php batch-processing

我有一个像

这样的清单
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",
.
.
)

我想分批进行卷曲调用,比如说 - 4.在这种情况下,我想要像 -

[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",

PHP中是否有一个内置/常用的方法可以简化这个? 我编写的 C like 代码太复杂而无法调试 -

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 个答案:

答案 0 :(得分:2)

使用 array_chunk 功能 例如:

   $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));

它会将其分成每个数组中的元素