我有两个数组。第一个数组是names,其中包含5个名称。第二个数组是groups,其中包含3个分组。我想循环遍历这两个数组,并将每个索引名称设置为索引组。如果组索引完成,则希望重新开始第二个数组。
我尝试在达到最后一项时将组索引重置为0,但它不起作用。
我尝试在达到最后一项时将组索引重置为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>";
}
当达到第四个名称项时,组项应为1。有没有办法做到这一点?
期望产出
John - 1
Jane - 2
George - 3
Jim - 1
Jack - 2
提前感谢你