2015-07-21 58 views
2

我需要知道「階段」出現的次數以及是否已完成。元素在多維數組中顯示的次數

的最終目標是要表明:

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

任務:

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

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

階段:

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

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

) 

編輯

所以,在你們回答的幫助下,我設法做到了!結果如下:

  /** 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'; 
      } 
+1

*了一個多小時*因此,有你寫了一些代碼,並試圖東西或做你只是盯着代碼一個小時? – Rizier123

+0

有一點,我嘗試了數十件事,每隔15分鐘左右我就會停下來想想我該怎麼做......只是找不到它 –

+0

然後告訴我們1你的嘗試,我們會幫助你實現它的工作。 – Rizier123

回答

1

有點慢到平局,但因爲我已經寫了,這裏是我的答案了。它總是有益的,看看大多數編程任務可以做幾種方法:

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

我知道你已經完成了,但對於相位陣列 - 你就應該把在原來的問題,如果你想它的工作進入答案。但是,我的建議是使用階段的ID作爲階段數組中的索引名稱。我發佈的解決方案也可以使用 - 然後您可以在最終的foreach循環中使用$階段來顯示標題或創建時間。

+0

嘿馬克,請閱讀我在吉姆的回答中留下的評論,這也適合你! –

+0

馬克,我更新了我的問題與實際$相數組,以防萬一你想更新你的答案...非常感謝! –

+0

我仍然試圖適應它,但沒有運氣,它比我想象的要困難...你介意看看我在問題中添加的$相位數組嗎? –

1

這是你在追求什麼?

我假設你想根據你的原始數組計算百分比。

<?php 

$tasks = array(
    array(
     'phase' => 1, 
     'tasks_status' => 'completed' 
    ), 
    array(
     'phase' => 1, 
     'tasks_status' => 'pending' 
    ), 
    array(
     'phase' => 2, 
     'tasks_status' => 'pending' 
    ), 
); 

$phases_arr = array(); 
foreach($tasks as $task) { 
    if(!array_key_exists($task['phase'], $phases_arr)) { 
     $phases_arr[$task['phase']] = array(); 
    } 
    $phases_arr[$task['phase']][] = $task; 
} 

$phases = array(); 

foreach($phases_arr as $phase_num => $phase) { 
    $p = array(
     'phase' => $phase_num, 
     'total' => count($phase), 
     'complete' => 0, 
     'percent' => 0 
    ); 

    foreach($phase as $task) { 
     if($task['complete']) $p['complete']++; 
    } 

    $p['percent'] = ($p['complete']/$p['total']) * 100; 
    $phases[$phase_num] = $p; 
} 

foreach($phases as $phase) { 
    echo "<p>Phase {$phase['phase']}: {$phase['percent']}% completed</p>"; 
} 
+1

認真的傢伙,我真的很感激。我在這裏調整了現場版本,我很快就會投出最好的答案,但無論如何,你們都是最好的。我只是堅持這一點,我會研究你的代碼,並嘗試提高我的技能,謝謝! –

+0

這是非常粗糙,絕對可以改善!但是你非常歡迎:)! –

+0

我仍然試圖實現它,因爲我的$相位數組有點不同,我已經更新了問題以顯示它,以防萬一您想更新您的答案 –

1

你的輸入,

$tasks = array(array('phase' => 1, 'tasks_status' => 'completed'), 
       array('phase' => 1, 'tasks_status' => 'pending'), 
       array('phase' => 2, 'tasks_status' => 'pending'), 
     ); 

考慮這一點,

$phase = array(); 

foreach ($tasks as $key => $value) 
{ 
    $phase[$value['phase']][] = $value['tasks_status'] == 'completed' ? 50 : 0 ; 
} 

function get_phase_percentage($i) 
{ 
    global $phase; 
    if(isset($phase[$i]) && is_array($phase[$i])) 
     return array_sum($phase[$i]); 
    return 0; 
} 

使用方法如下,

Phase 1: <?php echo get_phase_percentage(1)?>% completed 
Phase 2: <?php echo get_phase_percentage(2)?>% completed 
+0

非常感謝病毒,我試圖實現它 - 我已經發布了我的$階段數組,以防萬一你想更新你的答案 - 再次感謝 –

+0

病毒,我沒有讓它在我的$階段工作數組,我已經更新了這個問題,你介意看一下嗎? –

+0

謝謝,我做到了!我用最終的代碼編輯了我的問題。謝謝! –