简体   繁体   中英

How can I loop through this array and get first elements?

OK, pulling my hair out again today. I need to loop through this array to get the keys for the elements seen here as "Escalations, "Premium", "Standard", etc... so I can display them in a table.

I've tried this and all it gives me "abandoned", I can't figure out how to move up in the hierarchy.

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'];
}

HERE IS A 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>";
}

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

I think you're looking for this syntax:

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

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

}

Hope you control where the key name is coming from, as this is obviously vulnerable to XSS problems.

$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>';
 }

Don't see anything wrong with your code but it's way longer and messier than need be.

Edit: Actually it sounds like you're looping through the second dimension of your array, where is the code before your existing foreach?

i suggest add a counter if you want to do something about the 2nd until the last or stop the foreach loop after the 1st value if you dont have something to do with the 2nd until the last

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
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM