Find Minimum Value of an Array in PHP

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Arrays

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 with count($array), and simple CLI output using echo.
  • 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.

Runs the same comparison logic in JavaScript in your browser (no compile needed).

Live result
Press “Find minimum”.

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

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 min
1

Find minimum (reference program)

Matches the classic interview shape: a helper findMin, a sample array, and one-pass comparison logic. The sample minimum is 2.

c
<?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.

2

When every element is negative

Smallest still means “leftmost on the number line.” Here the minimum is −9, not −1.

c
<?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

It is the smallest number in the list. If you wrote all values on a line, it is the one farthest to the left on the number line (the most negative, or the smallest positive if everything is positive).
You need a first guess taken from the data. The first slot is a fair starting point. Then each later number can replace that guess if it is smaller.
With zero elements there is no smallest value. Real programs should check the length first and avoid reading arr[0]. This lesson assumes at least one element so the idea stays simple.
Yes. You still begin with the first element. Smaller means closer to negative infinity, so among {-9, -3, -1} the answer is -9.
That is fine. You still print that value once; duplicates do not change which number is smallest.
No. Sorting costs more work than one scan. To find only the minimum, walking the array once is enough.
You look at each of the n items once: O(n) time and O(1) extra space besides the array itself.

🔄 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

Empty

size == 0

There is no minimum; do not access arr[0] until you know the length is at least one.

One element

size == 1

The loop never runs; min_val stays that single element, which is correct.

⏱️ Time and space complexity

ApproachTimeExtra space
Single scanO(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.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful