PHP Minimum Value of an Array Program
Definition
What Is the Minimum Value of an Array?
The minimum is the smallest element in the array. For an unsorted array, scan once from left to right and keep a running best.
arr = [12, 45, 2, 42, 19]
min = 2
Idea:
min = arr[0]
for each later element x:
if x < min → min = x
Works for negatives too:
[-8, -1, -15] → min = -15
| Array | Minimum |
|---|
[12, 45, 2, 42, 19] | 2 |
[-8, -1, -15] | -15 |
[5] | 5 |
[] | undefined — handle empty |
Approach
How to Find the Minimum in PHP
Four clear steps — one linear pass is enough.
- Reject an empty array (no minimum exists).
- Set
$min = $arr[0] as the first candidate. - Loop from index
1 to the end. - Whenever
$arr[$i] < $min, update $min (optionally track the index too).
| Method | Idea | Time | Best for |
|---|
| Linear scan | Running minimum with < | O(n) | Interviews, unsorted arrays |
| Track index | Same scan, also store the position | O(n) | When you need where the min is |
Pseudocode
function FindMin(arr):
if length(arr) == 0:
error "empty array"
min = arr[0]
for i from 1 to length(arr) - 1:
if arr[i] < min:
min = arr[i]
return min
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Start | $min = $arr[0] |
| Update | if $arr[$i] < $min |
| Empty | Validate before reading $arr[0] |
| Seed | Never start with 0 — fails for all-positive arrays |
| Built-in | min($arr) exists — prefer a loop in interviews |
| Classic check | [12, 45, 2, 42, 19] → 2 |
Try it
Live Preview
Pick a sample array to see the running minimum after each step.
Trace
Worked Walkthrough — [12, 45, 2, 42, 19]
Start at the first element and update whenever a smaller value appears.
min = 12
i=1 → 45 ≥ 12 → no change
i=2 → 2 < 12 → min = 2
i=3 → 42 ≥ 2 → no change
i=4 → 19 ≥ 2 → no change
Minimum = 2
Code
PHP Programs
Start with a simple running minimum. Then track the index when you also need the position.
Example 1 — Linear Scan
Keep a running minimum and update it whenever a smaller element appears.
<?php
function findMin(array $arr): int
{
if (count($arr) === 0) {
throw new InvalidArgumentException("Array must not be empty.");
}
$min = $arr[0];
$n = count($arr);
for ($i = 1; $i < $n; $i++) {
if ($arr[$i] < $min) {
$min = $arr[$i];
}
}
return $min;
}
$numbers = [12, 45, 2, 42, 19];
echo "Minimum = " . findMin($numbers) . "\n";
How It Works
- Reject an empty array before reading
$arr[0]. - Seed
$min with the first element. - Loop from index
1 through the rest of the array. - Update when
$arr[$i] < $min. - Return the final running minimum after one pass.
Example 2 — Track Index
Same scan, but also remember the first index where the minimum appears.
<?php
function findMinIndex(array $arr): int
{
if (count($arr) === 0) {
throw new InvalidArgumentException("Array must not be empty.");
}
$minIndex = 0;
$n = count($arr);
for ($i = 1; $i < $n; $i++) {
if ($arr[$i] < $arr[$minIndex]) {
$minIndex = $i;
}
}
return $minIndex;
}
$numbers = [12, 45, 2, 42, 19];
$index = findMinIndex($numbers);
echo "Min = " . $numbers[$index] . " at index " . $index . "\n";
How It Works
- Start with
$minIndex = 0 (first element is the candidate). - Compare each later element to
$arr[$minIndex]. - When a smaller value appears, store that new index.
- Ties keep the earlier index because the test uses strict
<. - Read the value with
$numbers[$index] after the scan.
Edge Cases & Pitfalls
Check these before calling the solution done.
EmptyNo minimum
Validate count($arr) before using $arr[0].
Seed with 0Wrong for positives
Seeding $min = 0 fails when every element is positive.
DuplicatesSame answer
Multiple copies of the min still return that value.
SortingOverkill
One scan is enough; sorting is usually slower for this goal.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Linear scan | O(n) | O(1) |
| Track index | O(n) | O(1) |
You need about n − 1 comparisons in the worst case. One pass is optimal for an unsorted array.
Remember
Key Takeaways
Pattern: seed with $arr[0], then scan with <.
Empty: validate before reading the first element.
Index: same loop can track where the min sits.
Test: [12, 45, 2, 42, 19] → 2.
One line: one pass, keep the smaller value — that is the minimum.
Frequently Asked Questions
The first element is a valid candidate. Then every later element is compared against that running minimum.
An empty array has no minimum. Validate count($arr) > 0 and throw or return early.
Yes. The minimum is still the smallest value (for example 2 in [12, 45, 2, 19]).
min($arr) works in production. In interviews, show the linear scan so you prove you understand the idea.
One pass over n elements: O(n) time and O(1) extra space.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful