PHP Split Array into Smaller Even Arrays

php split array into smaller even arrays

EDIT: There's array_chunk, which does just that.

Well, I didn't feel like debugging, so I wrote a version with array_reduce:

$pergroup = 2;
$redfunc = function ($partial, $elem) use ($pergroup) {
$groupCount = count($partial);
if ($groupCount == 0 || count(end($partial)) == $pergroup)
$partial[] = array($elem);
else
$partial[$groupCount-1][] = $elem;

return $partial;
};

$arr = array(1,2,3,4,5);

print_r(array_reduce($arr, $redfunc, array()));

gives

Array
(
[0] => Array
(
[0] => 1
[1] => 2
)

[1] => Array
(
[0] => 3
[1] => 4
)

[2] => Array
(
[0] => 5
)

)

Splitting of array into multiple arrays

If you need such a variable array length of chunks then you can create your own functionality using simple foreach and array_splice like as

$array=[1,2,3,4,5,6,7,8,9,10];

$arr = [3,3,4];
$result_arr = [];
foreach($arr as $k => $v){
$result_arr[$k] = array_splice($array,0,$v);
}
print_R($result_arr);

Output:

Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)

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

[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 10
)

)

Divide/Split an array into two arrays one with even numbers and other with odd numbers

Your error is in if condition, you want to check if the number is odd or even, you have to use modulus % operator. So your code becomes like this

<?php $array = array(1,2,3,4,5,6);
$length = count($array);
$even = array();
for($i=0; $i < $length; $i++){
if($array[$i]%2 == 0){
$even[] = $array[$i];
}
else{
$odd[] = $array[$i];
}
}
print_r($even);
echo "<br/>";
print_r($odd);

splitting a large array into smaller arrays based on the key names

You could do this:

$byLocation = array();
foreach ($arr as $key => $item) {
if (!isset($byLocation[$item['location']])) {
$byLocation[$item['location']] = array();
}
$byLocation[$item['location']][$key] = $item;
}

Then for example $byLocation['.co.uk'][22] is the first item of your original array. If you don’t want to maintain the original key, just omit it and use [] instead.

PHP - Split an array into chunks based on its content

Initialize $chunk to an array containing the first element. Then loop through the rest of the array, comparing the current element to what's in $chunk. If it's different, process $chunk and reset it.

$chunk = array[$array[0]];
for ($i = 1; $i < count($array); $i++) {
if ($array[$i] != $chunk[0]) {
// use $chunk for something
$chunk = array($array[$i]);
} else {
$chunk[] = $array[$i];
}
}
// process the last $chunk here when loop is done

Split array into a specific number of chunks

You can try

$input_array = array(
'a',
'b',
'c',
'd',
'e'
);

print_r(partition($input_array, 4));

Output

Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[0] => c
)

[2] => Array
(
[0] => d
)

[3] => Array
(
[0] => e
)

)

Function Used

/**
*
* @param Array $list
* @param int $p
* @return multitype:multitype:
* @link http://www.php.net/manual/en/function.array-chunk.php#75022
*/
function partition(Array $list, $p) {
$listlen = count($list);
$partlen = floor($listlen / $p);
$partrem = $listlen % $p;
$partition = array();
$mark = 0;
for($px = 0; $px < $p; $px ++) {
$incr = ($px < $partrem) ? $partlen + 1 : $partlen;
$partition[$px] = array_slice($list, $mark, $incr);
$mark += $incr;
}
return $partition;
}

Split an Array into N Arrays - PHP

This simple function would work for you:

Usage

$array = range("a", "r"); // same as your array
print_r(alternate_chunck($array,12));

Output

Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[0] => c
[1] => d
)

[2] => Array
(
[0] => e
[1] => f
)

[3] => Array
(
[0] => g
[1] => h
)

[4] => Array
(
[0] => i
[1] => j
)

[5] => Array
(
[0] => k
[1] => l
)

[6] => Array
(
[0] => m
)

[7] => Array
(
[0] => n
)

[8] => Array
(
[0] => o
)

[9] => Array
(
[0] => p
)

[10] => Array
(
[0] => q
)

[11] => Array
(
[0] => r
)

)

Update The above might not be useful for most cases ... here is another type of chunk

$array = range("a", "r"); // same as your array
print_r(fill_chunck($array, 5));

Output

Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)

[1] => Array
(
[0] => e
[1] => f
[2] => g
[3] => h
)

[2] => Array
(
[0] => i
[1] => j
[2] => k
[3] => l
)

[3] => Array
(
[0] => m
[1] => n
[2] => o
[3] => p
)

[4] => Array
(
[0] => q
[1] => r
)

)

This would make sure the group at no time is more that 5 elements where the other one has no limitation

Function Used

function alternate_chunck($array, $parts) {
$t = 0;
$result = array();
$max = ceil(count($array) / $parts);
foreach(array_chunk($array, $max) as $v) {
if ($t < $parts) {
$result[] = $v;
} else {
foreach($v as $d) {
$result[] = array($d);
}
}
$t += count($v);
}
return $result;
}

function fill_chunck($array, $parts) {
$t = 0;
$result = array_fill(0, $parts - 1, array());
$max = ceil(count($array) / $parts);
foreach($array as $v) {
count($result[$t]) >= $max and $t ++;
$result[$t][] = $v;
}
return $result;
}

Split an array into equal parts with a maximum of 6 items per array

I guess this is what you are looking for. Not exactly beautiful, but working:

<?php
$size = 13;
$step = 6;

$input = array_keys(array_fill(0, $size, null));
$count = ceil($size / $step);
$chunk = floor($size / $count);
$bonus = $size % $count;

for ($i = 0; $i < $count; $i++) {
$output[] =
$i == 0 ?
array_slice($input, $i * $chunk, $chunk + $bonus) :
array_slice($input, $i * $chunk + $bonus, $chunk);
}

print_r($output);

Here $size is the size of your array and $step is the size of the chunks cut from that array. You can play around with those values.

An example output with the settings above would be:

Array
(
[0] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)

[1] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)

[2] => Array
(
[0] => 9
[1] => 10
[2] => 11
[3] => 12
)
)


Related Topics



Leave a reply



Submit