我有两个数组。第一个数组是名称,它有5个名称。第二个数组是组,它具有3个组。我想遍历两个数组并将每个索引名称设置为索引组。如果组索引完成,我想重新启动第二个数组。
我尝试将组索引重置为0(如果到达最后一项),但是它不起作用。
$names = [
'John',
'Jane',
'George',
'Jim',
'Jack'
];
$groups = [
'1',
'2',
'3'
];
foreach ($names as $index => $name) {
$result = "The student: " . $name . " belongs to the: " .$groups[$index]. "group" ;
echo ($result);
echo "<br>";
echo "<br>";
}
Run Code Online (Sandbox Code Playgroud)
当它到达第四个名称项时,分组项应为1。是否可以进行这种思考?
预期结果
John - 1
Jane - 2
George - 3
Jim - 1
Jack - 2
Run Code Online (Sandbox Code Playgroud)
先感谢您
假设我有这个数组:
$myArray = array(a, b, c, d, e, f, g);
Run Code Online (Sandbox Code Playgroud)
我有一个开始指示符,$startpos其可能的值可以是从0到myArray的元素数量的任何值.
因此,如果$startpos = 0,所需的打印结果将是a, b, c, d, e, f, g
如果$startpos = 2,所需的打印结果将是c, d, e, f, g, a, b
如果$startpos = 5,所需的打印结果将是f, g, a, b, c, d, e
我一直在通过SO搜索一个php内置或自定义函数(类似问题,在选择元素时将数组视为圆形数组 - PHP)并查看http://www.w3schools.com/php/php_ref_array. asp,但我没有得到理想的结果.有人可以给我一个建议吗?
我需要能够循环一个项目数组并从另一个数组中给它们一个值,我无法理解它.
我的阵列
$myarray = array('a','b','c');
Run Code Online (Sandbox Code Playgroud)
假设我有一个foreach循环,我总共循环了6个项目.
如何获得以下输出
item1 = a
item2 = b
item3 = c
item4 = a
item5 = b
item6 = c
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样.
$myarray = array('a','b','c');
$items = array(0,1,2,3,4,5,6);
foreach ($items as $item) {
echo $myarray[$item];
}
Run Code Online (Sandbox Code Playgroud)
在线示例. http://codepad.viper-7.com/V6P238
我当然希望能够循环无数次