4

I have two arrays. The first array is names and it has 5 names. The second array is groups and it has 3 groups. I want to loop through both of the arrays and set each index name to the index group. If the group index finishes I want to restart the second array.

I tried reseting the group index to 0 if it reaches the last item but it doesn't work.

$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>";
}

When it reaches the fourth name item, the group item should be 1. Is there a way to do such think?

Expected outcome

John - 1

Jane - 2

George - 3

Jim - 1

Jack - 2

Thank you in advance

1
  • 1
    Can you share your expected output here Commented Apr 4, 2019 at 12:32

4 Answers 4

6

We assume that the array is numerically indexed (starting from 0), and that there are no indexes that are skipped (i.e. the $group array is always defined as range(1, $n);).

Then you can use the modulus operator % against the length of that array, as shown below.

foreach ($names as $index => $name) {
   $result = "The student: " . $name . " belongs to group " .$groups[$index % count($groups)]. ".\n";
   echo $result;
}
Sign up to request clarification or add additional context in comments.

Comments

4

You can modulo % the groups indexing.

$groups[$index % sizeOf($groups)]

<?php
$names = [
  'John',
  'Jane',
  'George',
  'Jim',
  'Jack'
];

$groups = [
  '1',
  '2',
  '3'
];

foreach ($names as $index => $name) {
   $result = "The student: " . $name . " belongs to the: " .$groups[$index%sizeOf($groups)]. "group" ;
   echo ($result);
   echo "<br>";
   echo "<br>";
}

result: https://3v4l.org/LnOHAK

The student: John belongs to the: 1group<br><br>The student: Jane belongs to the: 2group<br><br>The student: George belongs to the: 3group<br><br>The student: Jim belo

Comments

0

Try this! In this code if you add a new group no have be problem.

<?php

$names = [
  'John',
  'Jane',
  'George',
  'Jim',
  'Jack'
];

$groups = [
  '1',
  '2',
  '3'
];

foreach ($names as $index => $name) {

   $result = "The student: " . $name . " belongs to the: " .$groups[$index % count($groups)]. "group" ;
   echo ($result);
   echo "<br>";
   echo "<br>";
}

Comments

0

This will work for any kind of array

$names = [
  'John',
  'Jane',
  'George',
  'Jim',
  'Jack'
];

global $groups;

$groups = [
  '1',
  '2',
  '3'
];

$newArr = array_combine(
            $names, 
              array_merge(
                  $groups,
                  array_map(
                    function($v){
                      global $groups;
                      return $groups[$v%count($groups)]; 
                    }, 
                   array_keys(array_diff_key($names, $groups))
                 )
              )
          );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.