2

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" } } 
3
  • 1
    are you looking for foreach ($values as $key => value) {}? Commented Oct 17, 2013 at 15:43
  • I'm looking to put "Escalations" etc into the table where it says key($result). Commented Oct 17, 2013 at 15:49
  • 1
    +1 for beeing person 1 of 1000 properly concatenating variables and strings Commented Oct 17, 2013 at 15:56

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This makes no sense - what does XSS have to do with anything the OP posted, and how does switching to key=>value syntax prevent XSS as opposed to using key() function? I agree this is simpler, looks nicer, but it has nothing to do with XSS
this works well on it's own but it doesn't put the element in the table where I need it.
Solution: foreach($results as $header => $result){ // To calculate precentages $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>" . $header. "</td>";//key ... }
@Jessica: OP had key($result), which was wrong. Even key($results) would have been wrong as foreach loops in PHP create a copy of the array, and use a different array position than the one key would use. The XSS comment was unrelated to the OP's question, but I felt it was relevant to add in case OP missed it and the contents of the $results array came from for example a database or otherwise from the user. Makes sense?
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>'; } 

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?

2 Comments

Do a var_dump($result); right inside your foreach. What does $result actually contain?
Looks fine to me, I don't see how your code can cause the problem you described.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.