【问题标题】:Outputting array in sections PHP在 PHP 部分中输出数组
【发布时间】:2017-03-21 09:27:09
【问题描述】:

所以我在学校工作控制评估中使用 php,我需要在部分中输出一组用户 ID。

但是它必须以这种方式运行:

  • 数组必须位于 UIDS 的 3 段中
  • UIDS 不能重复 并且数组部分将添加到时间表中。

所以有 10 个 UIDS,它们必须按如下方式拆分和分类:

Split 1  // The remainder is also not forgotten about
1,4,7,10

Split 2  // Vertical assorted 
2,5,8

Split 3
3,6,9

【问题讨论】:

    标签: php arrays sorting


    【解决方案1】:

    你可以只使用模数

    <?php
    
    $uids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    
    $split = [
        0 => [],
        1 => [],
        2 => [],
    ];
    
    foreach ($uids as $index => $value) {
        $split[$index % 3][] = $value;
    }
    
    var_dump($split);
    

    输出:

    array(3) {
      [0]=>
      array(4) {
        [0]=> int(1)
        [1]=> int(4)
        [2]=> int(7)
        [3]=> int(10)
      }
      [1]=>
      array(3) {
        [0]=> int(2)
        [1]=> int(5)
        [2]=> int(8)
      }
      [2]=>
      array(3) {
        [0]=> int(3)
        [1]=> int(6)
        [2]=> int(9)
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2021-04-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多