2

よし、今日も髪を抜く。この配列をループして、ここで「エスカレーション」、「プレミアム」、「標準」などとして表示される要素のキーを取得する必要があるため、それらをテーブルに表示できます。

私はこれを試してみましたが、すべてが「放棄」され、階層を上に移動する方法がわかりません。

foreach($calls as $call){
$results[ $call['queue_name'] ][ $call['type'] ] = $call['calls'];
$totalCalls += $call['calls'];
$typeTotals[ $call['type'] ] = isset( $typeTotals[ $call['type'] ]) ? $typeTotals[ $call['type'] ] + $call['calls'] : $call['calls'];
}

ここにvar_dumpがあります:

array(5) { 
["Escalations"]=> array(3) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" 
    ["redirected"]=> string(1) "1" } 

["Premium"]=> array(3) { 
    ["abandoned"]=> string(1) "7" 
    ["completed"]=> string(2) "29" 
    ["redirected"]=> string(1) "6" } 

["Standard"]=> array(3) { 
    ["abandoned"]=> string(2) "14" 
    ["completed"]=> string(2) "41" 
    ["redirected"]=> string(2) "53" } 

["Wingate Queue"]=> array(2) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" } 

["WorldMark"]=> array(3) { 
    ["abandoned"]=> string(2) "32" 
    ["completed"]=> string(3) "100" 
    ["redirected"]=> string(2) "82" } } 


array(5) { 
["Escalations"]=> array(3) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" 
    ["redirected"]=> string(1) "1" } 

["Premium"]=> array(3) { 
    ["abandoned"]=> string(1) "7" 
    ["completed"]=> string(2) "29" 
    ["redirected"]=> string(1) "6" } 

["Standard"]=> array(3) { 
    ["abandoned"]=> string(2) "14" 
    ["completed"]=> string(2) "41" 
    ["redirected"]=> string(2) "53" } 

["Wingate Queue"]=> array(2) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" } 

["WorldMark"]=> array(3) { 
    ["abandoned"]=> string(2) "32" 
    ["completed"]=> string(3) "100" 
    ["redirected"]=> string(2) "82" } } 


array(5) { 
["Escalations"]=> array(3) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" 
    ["redirected"]=> string(1) "1" } 

["Premium"]=> array(3) { 
    ["abandoned"]=> string(1) "7" 
    ["completed"]=> string(2) "29" 
    ["redirected"]=> string(1) "6" } 

["Standard"]=> array(3) { 
    ["abandoned"]=> string(2) "14" 
    ["completed"]=> string(2) "41" 
    ["redirected"]=> string(2) "53" } 

["Wingate Queue"]=> array(2) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" } 

["WorldMark"]=> array(3) { 
    ["abandoned"]=> string(2) "32" 
    ["completed"]=> string(3) "100" 
    ["redirected"]=> string(2) "82" } }


array(5) { 
["Escalations"]=> array(3) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" 
    ["redirected"]=> string(1) "1" } 

["Premium"]=> array(3) { 
    ["abandoned"]=> string(1) "7" 
    ["completed"]=> string(2) "29" 
    ["redirected"]=> string(1) "6" } 

["Standard"]=> array(3) { 
    ["abandoned"]=> string(2) "14" 
    ["completed"]=> string(2) "41" 
    ["redirected"]=> string(2) "53" } 

["Wingate Queue"]=> array(2) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" } 

["WorldMark"]=> array(3) { 
    ["abandoned"]=> string(2) "32" 
    ["completed"]=> string(3) "100" 
    ["redirected"]=> string(2) "82" } }


array(5) { 
["Escalations"]=> array(3) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" 
    ["redirected"]=> string(1) "1" } 

["Premium"]=> array(3) { 
    ["abandoned"]=> string(1) "7" 
    ["completed"]=> string(2) "29" 
    ["redirected"]=> string(1) "6" } 

["Standard"]=> array(3) { 
    ["abandoned"]=> string(2) "14" 
    ["completed"]=> string(2) "41" 
    ["redirected"]=> string(2) "53" } 

["Wingate Queue"]=> array(2) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" }

["WorldMark"]=> array(3) { 
    ["abandoned"]=> string(2) "32" 
    ["completed"]=> string(3) "100" 
    ["redirected"]=> string(2) "82" } } 


 foreach($results as $result){
$perAbandoned = $totalCalls != 0 ? round( ($result['abandoned'] / $totalCalls) * 100 ) : 0;
$perRedirected = $totalCalls != 0 ? round( ($result['redirected'] / $totalCalls) * 100) : 0;
$perAnswered = $totalCalls != 0 ? round( ($result['completed'] / $totalCalls) * 100 ) : 0;  


echo  "<tr>";
echo "<td>" . key($result) . "</td>";//key
echo "<td>" . $totalCalls . "</td>";
echo "<td>" . $result['completed'] . "</td>";
echo "<td>" . $perAnswered . "%</td>";
echo "<td>" . $result['abandoned'] . "</td>";
echo "<td>" . $perAbandoned . "%</td>";
echo "<td>" . $result['redirected'] . "</td>";
echo "<td>" . $perRedirected . "%</td>";
echo "<td>techs logged in</td>";
echo  "</tr>";
}

アレイ:

array(5) { 
["Escalations"]=> array(3) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" 
    ["redirected"]=> string(1) "1" } 

["Premium"]=> array(3) { 
    ["abandoned"]=> string(1) "7" 
    ["completed"]=> string(2) "29" 
    ["redirected"]=> string(1) "6" } 

["Standard"]=> array(3) { 
    ["abandoned"]=> string(2) "14" 
    ["completed"]=> string(2) "41" 
    ["redirected"]=> string(2) "53" } 

["Wingate Queue"]=> array(2) { 
    ["abandoned"]=> string(1) "2" 
    ["completed"]=> string(1) "3" } 

["WorldMark"]=> array(3) { 
    ["abandoned"]=> string(2) "32" 
    ["completed"]=> string(3) "100" 
    ["redirected"]=> string(2) "82" } } 
4

3 に答える 3

1
$types = ['abandoned', 'completed', 'redirected'];
foreach($results as $key=>$result){
   echo '<tr><td>'.$key.'</td><td>'.$totalCalls.'</td>';
   foreach($types AS $type){
      echo '<td>'.$result[$type].'</td><td>';
      echo $totalCalls != 0 ? round( ($result[$type] / $totalCalls) * 100 ) : 0;
      echo '%</td>';
   }
   echo '</tr>';
 }

コードに問題はありませんが、必要以上に長くて厄介です。

編集:実際には、配列の 2 番目の次元をループしているように聞こえますが、既存の foreach の前のコードはどこにありますか?

于 2013-10-17T15:46:03.023 に答える
0

最後まで2番目について何かしたい場合はカウンターを追加するか、最後まで2番目とは関係がない場合は1番目の値の後にforeachループを停止することをお勧めします

foreach($results as $value) 
{

$counter=1;
if($counter > 1)
{
//do what you want on the 2nd until the end value
}else
{
echo "this is the 1st value";
$first=$value; //or store it
}

}
于 2013-10-17T15:49:15.190 に答える