如何遍历此数组并获取第一个元素?

时间:2013-10-17 15:40:15

标签: php

好的,今天再把头发拉出来。我需要遍历这个数组来获取这里看到的元素的键“Escalations”,Premium“,”Standard“等等......所以我可以在表格中显示它们。

我已经尝试了这一切,而且它让我“被遗弃”,我无法弄清楚如何在层次结构中向上移动。

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:

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

3 个答案:

答案 0 :(得分:2)

我认为你正在寻找这种语法:

foreach($results as $header => $result) {

   echo "<td>" . $header . "</td>";//key

}

希望您能够控制密钥名称的来源,因为这显然容易受到XSS问题的影响。

答案 1 :(得分: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>';
 }

您的代码没有看到任何问题,但它比需要的更长,更麻烦。

编辑:实际上听起来你正在循环遍历阵列的第二维,在你现有的foreach之前的代码在哪里?

答案 2 :(得分:0)

我建议你添加一个计数器,如果你想做第二个直到最后一个 如果你没有与之相关的话,或者在第一个值之后停止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
}

}