How to break an array into a specified number of smaller arrays?
I have an array 开发者_StackOverflowcontaining a lot of records. I need to split it into 4 smaller arrays. How can I do this?
Use the array_chunk
function: http://php.net/manual/en/function.array-chunk.php
$numberOfSmallerArrays = 4;
$arrayOfSmallerArrays = array_chunk($largeArray, ceil(count($largeArray) / $numberOfSmallerArrays));
Try array_chunk.
Find a criteria, and to something like:
$vCurrentArr = array(1, 2, 3, 4,4);
$vArray1 = array();
$vArray2 = array();
$vArray3 = array();
$vArray4 = array();
foreach($arr as &$value) {
if($value=1) {
$vArray1[count($vArray1)+1]=$value;
} else if($value=2) {
$vArray2[count($vArray2)+1]=$value;
} else if($value=3) {
$vArray3[count($vArray3)+1]=$value;
} else if($value=4) {
$vArray4[count($vArray4)+1]=$value;
}
}
精彩评论