【问题标题】:PHP Loop two arrays if index of one is last rewind arrayPHP循环两个数组,如果一个索引是最后一个倒带数组
【发布时间】:2019-04-04 12:31:15
【问题描述】:

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

到第四个name item时,group item应该是1。有没有办法这样想?

预期结果

John - 1

Jane - 2

George - 3

Jim - 1

Jack - 2

提前谢谢你

【问题讨论】:

  • 你能在这里分享你的预期输出吗

标签: php arrays foreach


【解决方案1】:

我们假设数组是数字索引的(从 0 开始),并且没有被跳过的索引(即$group 数组总是定义为range(1, $n);)。

然后您可以使用模运算符% 来处理该数组的长度,如下所示。

foreach ($names as $index => $name) {
   $result = "The student: " . $name . " belongs to group " .$groups[$index % count($groups)]. ".\n";
   echo $result;
}

【讨论】:

    【解决方案2】:

    您可以对组索引取模 %

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

    结果: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
    

    【讨论】:

      【解决方案3】:

      试试这个!在此代码中,如果您添加一个新组,则没有问题。

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

      【讨论】:

        【解决方案4】:

        这适用于任何类型的数组

        $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))
                         )
                      )
                  );
        

        【讨论】:

          猜你喜欢
          • 2012-11-25
          • 2018-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多