duanhun3273 2015-07-21 22:55
浏览 15
已采纳

元素在多维数组中显示的次数

I need to know how many times a "phase" shows up, and if it's completed or not.

The final objective is to show:

Phase 1: 50% completed
Phase 2: 0% completed

Tasks:

Array
(
    [0] => Array
        (
            [phase] => 1
            [tasks_status] => completed
        )

    [1] => Array
        (
            [phase] => 1
            [tasks_status] => pending
        )
    [2] => Array
        (
            [phase] => 2
            [tasks_status] => pending
        )
)

Phases:

Array
(
    [0] => Array
        (
            [title] => Briefing e Planejamento
            [id] => 1
            [created] => 1437436800
        )

    [1] => Array
        (
            [title] => Rabiscos e Design
            [id] => 2
            [created] => 1438128000
        )

)

edit

So, with the help of you guys who answered, I've managed to do it! Here's the result:

            /** Define wich tasks has been completed **/
            foreach ($tasks as $task) {
                if ($task['status'] == "completed") {
                    foreach ($fases as &$fase) {
                        if ($fase['id'] == $task['fase_id']) {
                            $fase['numero_total_fases']++;
                            $fase['completed']++;
                        }
                    }
                } elseif ($task['status'] == "pending") {
                    foreach ($fases as &$fase) {
                        if ($fase['id'] == $task['fase_id']) {
                            $fase['numero_total_fases']++;
                            $fase['pending']++;
                        }
                    }
                } elseif ($task['status'] == "behind schedule") {
                    foreach ($fases as &$fase) {
                        if ($fase['id'] == $task['fase_id']) {
                            $fase['numero_total_fases']++;
                            $fase['behind schedule']++;
                        }
                    }
                }
                unset($fase);
            }

            /** Define progress percentage **/
            foreach ($fases as &$fase) {
                $fase['progresso'] = round(($fase['completed'] / ($fase['numero_total_fases'])) * 100);
            }

            /** Print phases progress **/
            $f = 1;
            foreach ($fases as $fase) {
                echo 'Fase '.$f.' - '.$fase['title'].' = '.$fase['progresso'].'% completed';
            }
  • 写回答

3条回答 默认 最新

  • drutjkpsr67393592 2015-07-21 23:21
    关注

    A little slow to the draw, but since I already wrote it, here's my answer too. It's always helpful to see that most programming tasks can be done several ways:

    <?php 
      $tasks = array(
        array(
          'phase' => 1,
          'tasks_status' => 'completed'
        ),
        array(
          'phase' => 1,
          'tasks_status' => 'pending'
        ),
        array(
          'phase' => 2,
          'tasks_status' => 'pending'
        )
      );
      //$phases = array();
      $phases = array(
        1=>array(
          'title'=>'Briefing e Planejamento',
          'id'=>1,
          'created'=>1437436800
        ),
        2=>array(
          'title'=>'Rabiscos e Design',
          'id'=>2,
          'created'=>1438128000
        )
      );
    
      foreach($tasks as $key=>$task){
        if(isset($phases[$task['phase']])){
          $thisPhase = $phases[$task['phase']];
        }
        else{
          $thisPhase = array(
            'completed' => 0,
            'pending' => 0
          );
        }
        $thisPhase[$task['tasks_status']]++;
        $phases[$task['phase']] = $thisPhase;
      }
    
      foreach($phases as $name=>$phase){
        echo $phase['title'].": ".round(($phase['completed'] / ($phase['completed'] + $phase['pending'])) * 100)."% completed <br>";
      }
    ?>
    

    I know you've already finished, but regarding the phases array - you should have put that in the original question if you wanted it worked into the answer. My suggestion, however, would be to use the ID of the phase as the index name in your phases array. The solution I posted will work with that too - and you can then use $phase in the final foreach loop to show the title or created time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分