【问题标题】:plus member of array dependent on the another array members加上依赖于另一个数组成员的数组成员
【发布时间】:2020-01-28 08:57:17
【问题描述】:


我的数据库中有一个表,如底部
课程     状态
ICDL         1
ICDL         1
ICDL         0
3DMAX     1
自动分类 1
AutoCAD     0

我想像这样显示日期表。


课程     致电     注册
ICDL          2         1
3DMAX      1         0
自动分类       1         1

我想删除重复的课程。 我也想加上她的课程名称的状态

请帮帮我

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

    这将是直接的方法:

    <?php
    $input = [
      [
        'course' => 'ICDL',
        'statuse' => 1,
      ],
      [
        'course' => 'ICDL',
        'statuse' => 1,
      ],
      [
        'course' => 'ICDL',
        'statuse' => 0,
      ],
      [
        'course' => '3DMAX',
        'statuse' => 1,
      ],
      [
        'course' => 'autocat',
        'statuse' => 1,
      ],
      [
        'course' => 'autocat',
        'statuse' => 0,
      ]
    ]
    ;
    
    $output = [];
    array_walk($input, function($entry) use (&$output) {
      $course = &$entry['course'];
    
      if (!array_key_exists($course, $output)) {
        $output[$course] = [
          'call' => 0,
          'register' => 0
        ];
      }
    
      switch($entry['statuse']) {
        case 1: 
          $action = 'call';
          break;
        case 0: 
          $action = 'register';
          break;
        default: 
          throw new Exception("undefined action '$action'!");
      }
      $output[$course][$action]++;
    });
    
    print_r($output);
    

    明显的输出是:

    Array
    (
        [ICDL] => Array
            (
                [call] => 2
                [register] => 1
            )
    
        [3DMAX] => Array
            (
                [call] => 1
                [register] => 0
            )
    
        [autocat] => Array
            (
                [call] => 1
                [register] => 1
            )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多