选择多个数组元素 [英] Selecting multiple array elements

查看:81
本文介绍了选择多个数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中是否有一种方法可以一次选择多个数组元素,例如这样,在for循环中,$ i =要选择的第一个集合的大小,然后后续递增表示从数组-?

Is there a way in PHP to select multiple array elements at once, e.g. such that in a for loop, $i = size of first set to be selected, and then subsequent increments represent selecting the next set of that size, from an array - ?

谢谢!

推荐答案

即而不是一次循环遍历一个数组元素,而是循环遍历选定的对(例如3个元素,然后对这3个元素做些事情).

I.e. instead of just looping through one array element at a time, but to loop through selected pairs instead (e.g. 3 elements, and then to do something to those 3).

有很多方法可以实现.
一个会是

there are many ways to do it.
one would be

$arr = array(1,2,3,4,5,6,7,8,9);
$new = array_chunk($arr,3);
foreach ($new as $chunk) {
  print_r($chunk);// 3 elements to do something with
}

这篇关于选择多个数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆