Reputation: 1909
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 = "";
}
Upvotes: 0
Views: 90
Reputation: 5260
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
Upvotes: 2