循环遍历数组块 php [重复]

问题描述 投票:0回答:1

我将数组分成块。我想问如何循环它们以便将每个块用作数组并打印它们的数据?这是到目前为止我的代码..

$chunks = array_chunk($newarray, 5, true);

foreach ($chunks as $index => $chunk) {


}
php arrays
1个回答
3
投票
$chunks = array_chunk($newarray, 5, true);

for ($i = 0; $i < count($chunks); $i++) {
   for ($n = 0; $n < count($chunks[$i]); $n++) { //or $n = 0; $n < 5; $n++ in your example
      print_r($chunks[$i][$n]);
   }
}

array_chunk()
默认情况下索引所有块 [0-..to 数组大小]

或与 foreach

foreach ($chunks as $index => $chunk)
    foreach ($chunk as $key => $value)
        print_r($index . $key . $value);
© www.soinside.com 2019 - 2024. All rights reserved.