Find Minimum Value of an Array in PHP
What you’ll learn
- What “minimum” means for a list of numbers, without assuming you already code every day.
- The usual one-pass trick: remember the best answer so far and update it when you see something smaller.
- A small reusable function
findMin, counting elements withcount($array), and simple CLI output usingecho. - A second example where every value is negative, plus a live preview that uses the same numbers as example 1.
Overview
Picture a row of numbers on the board. To find the smallest, you can walk from left to right and keep the best (“smallest so far”) in your head. A computer does the same with a variable—no sorting, no magic.
Two programs
A mixed list (answer 2) and a list of only negatives (answer −9).
Live preview
Try the button: it uses the same eight integers as example 1.
Interview hook
Say O(n) time, O(1) extra space, and mention empty arrays if the interviewer asks.
Prerequisites
for/foreach loops, arrays, and echo.
- Basic PHP syntax and indexed arrays like
$arr = [ ... ]. - Optional but handy:
count($arr)gives the array length.
The idea
Start by treating the first number as the smallest you have seen. For each next number, ask: “Is this smaller than what I am holding?” If yes, replace your answer. After you visit every position, what you hold is the minimum for the whole array.
One loop, one variable—easy to follow on paper and in an interview.
Live preview
Same eight integers as example 1: 12, 5, 7, 3, 2, 8, 10.
Algorithm
Goal: return the smallest value among n integers in a PHP indexed array.
Check length
For this tutorial, assume size >= 1. If the length can be zero, handle that before you read arr[0].
Initialize
Set min = arr[0].
Scan
For i from 1 to size - 1, if arr[i] < min, set min = arr[i].
Return
Return min.
📜 Pseudocode
function findMin(arr): // assume length >= 1
min ← arr[0]
for i from 1 to length(arr) - 1:
if arr[i] < min:
min ← arr[i]
return minFind minimum (reference program)
Matches the classic interview shape: a helper findMin, a sample array, and one-pass comparison logic. The sample minimum is 2.
<?php
function findMin(array $arr): int
{
$minVal = $arr[0];
for ($i = 1; $i < count($arr); $i++) {
if ($arr[$i] < $minVal) {
$minVal = $arr[$i];
}
}
return $minVal;
}
$array = [12, 5, 7, 3, 2, 8, 10];
$minValue = findMin($array);
echo "Minimum value in the array: $minValue\n";
?>Explanation
The running minimum starts at the first element and updates only when a smaller value appears. Because each element is checked once, this is clean and efficient for interview-style solutions.
When every element is negative
Smallest still means “leftmost on the number line.” Here the minimum is −9, not −1.
<?php
function findMin(array $arr): int
{
$minVal = $arr[0];
foreach ($arr as $value) {
if ($value < $minVal) {
$minVal = $value;
}
}
return $minVal;
}
$negatives = [-9, -3, -1, -7];
echo "Minimum (most negative): " . findMin($negatives) . "\n";
?>Notes
Parallel systems. You can split the array, find each part’s minimum, then take the minimum of those—same idea as parallel max, but with smaller-is-better.
Floating-point. The same loop works with double; in numerical code be careful with NaN and with comparing floats for exact equality.
❓ FAQ
🔄 Input / output
These programs use fixed numbers inside the code. To read values in PHP CLI, get a line with trim(fgets(STDIN)), split it, cast to integers, then pass the array to findMin.
Edge cases
size == 0
There is no minimum; do not access arr[0] until you know the length is at least one.
size == 1
The loop never runs; min_val stays that single element, which is correct.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Single scan | O(n) | O(1) |
Summary
- Algorithm: keep a running minimum while scanning once.
- Cost:
O(n)time,O(1)extra space. - Edge: decide what to do when
n = 0.
Finding the smallest value in an unsorted list still takes a single left-to-right pass: O(n) time and O(1) extra memory. You cannot do better in the worst case without extra structure, because any unseen element could be the new minimum.
8 people found this page helpful
