Codectionary / Developer documentation / PHP

Find Missing Values with array_diff

Compare values across lists while accounting for preserved keys and string-based equality.

What it is and why to use it

To find unfinished lessons, compare a list of planned lessons with the completed ones. array_diff() returns entries from the first array whose values are absent from the other arrays. It is a one-way difference: swapping the arguments answers a different question. Original keys and iteration order are preserved in the result.

How it works

The comparison considers values rather than their keys. Values are considered equal when their string representations are identical, so integer 1 and string "1" compare equal here. This is not PHP’s strict === comparison. For a strict membership comparison, filter the source with in_array($value, $other, true). Use simple scalar values; nested arrays require a different comparison strategy.

Common mistakes and limitations

A nonempty difference may have gaps in numeric keys; call array_values if a JSON list is required. Do not use array_diff as a strict type comparison or assume it removes only one occurrence of a matching value. Every matching occurrence in the first array is excluded.

Run the examples locally

Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.

The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.

These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.

Check your interpreter, lint and run one saved example

php -v
php -l example.php
php example.php

Continue learning

Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.

Syntax

$missing = array_diff($planned, $completed);

Examples

Find lessons still to finish

Subtract completed lesson names from a planned sequence.

<?php
declare(strict_types=1);

$planned = ['Arrays', 'Functions', 'Classes'];
$completed = ['Functions'];
$remaining = array_diff($planned, $completed);
foreach ($remaining as $position => $lesson) {
    echo $position, ': ', $lesson, PHP_EOL;
}
  1. Functions occurs in the completed list and is excluded from the result.
  2. Keys 0 and 2 are preserved, so the positions still refer to the original plan.
  3. The result is useful for a progress report. Reindex it separately when presenting a sequential API list.
  4. Expected output: 0: Arrays 2: Classes

Compare string equality with strict membership

Use mixed types deliberately to reveal an important comparison rule.

<?php
declare(strict_types=1);

$values = [1, '1', 2];
$other = ['1'];
$difference = array_values(array_diff($values, $other));
$strict = array_values(array_filter($values, fn($value) => !in_array($value, $other, true)));
echo json_encode($difference, JSON_THROW_ON_ERROR), PHP_EOL;
echo json_encode($strict, JSON_THROW_ON_ERROR), PHP_EOL;
  1. array_diff treats integer 1 and string "1" as the same string representation and excludes both.
  2. The callback uses strict in_array, so only the string is excluded and integer 1 remains.
  3. array_values reindexes both results for predictable JSON arrays. Choose the equality rule that matches your application’s data.
  4. Expected output: [2] [1,2]

Best practices

  • Name arrays so the direction of comparison is clear.
  • Use explicit strict membership checks for mixed-type identifiers.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading