我有一个通过 foreach
循环处理的数组。
foreach($images as $imageChoices) {
Do stuff here
}
如何限制循环只处理数组中的前 4 项?
最佳答案
array_slice()
可以使用函数。
foreach(array_slice($images, 0, 4) as $imageChoices) { … }
这使您能够只循环遍历所需的值,而无需计算到目前为止已经完成了多少。
关于php - 限制数组中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8907369/