简体   繁体   中英

How to break an array into a specified number of smaller arrays?

I have an array containing 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));

尝试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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM