用php写算法分割数组,不用array_chunk();算法如下所示。
<?php //$array 数组 //$size 每个数组的个数 //每个数组元素是否默认键值 function array_chunk_list($array, $size, $preserve_keys = false) { reset($array); $i = 0; foreach ($array as $key => $value) { // 是否存在这个值 if (! isset($newarray[$i])) { $newarray[$i] = array(); } if (count($newarray[$i]) < $size) { // 先判断的问题 if ($preserve_keys == false) { $newarray[$i][] = $value; } else { $newarray[$i][$key] = $value; } } else { $i++; if ($preserve_keys == false) { $newarray[$i][] = $value; } else { $newarray[$i][$key] = $value; } } } return $newarray; } $array=array(1,2,3,4,5,6,7); print_r(array_chunk_list($array, 2,true));

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
