array_key_last() — PHP Technical Interview Questions & Answers

array_key_last()

array_key_last()

  • array_key_last() is a PHP array helper. You pass an array, you get a new array or a boolean/count. Example: in_array(40, $marks), array_merge($a,$b), count($marks).
  • foreach is still how you loop. Functions don’t replace understanding [ ] vs ['name'=>].
  • Trap: expecting the original array to change when the function returns a copy (array_map).

Technical Interview Tip: array_key_last(): one array, one function call, say the result.

Code Examples for array_key_last()

array_key_last() — working example

<?php
// array_key_last()
$scores = [80, 90, 70];
$avg = array_sum($scores) / count($scores);
echo "avg=" . number_format($avg, 1) . "\n";
foreach ($scores as $s) {
  if ($s >= 80) echo "high: $s\n";
}

array_key_last(): scores array, print avg, then high marks. Say the output.

How to Explain array_key_last() to an Interviewer

What is array_key_last()?

A PHP array helper Try it on $marks = [72,40]