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 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录