小编Ris*_*aha的帖子

如果一个索引是最后一个倒带数组,则PHP循环两个数组

我有两个数组。第一个数组是名称,它有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)

先感谢您

php arrays foreach

4
推荐指数
1
解决办法
86
查看次数

标签 统计

arrays ×1

foreach ×1

php ×1