1.2.306. Could Use array_unique

Use array_unique() to collect unique elements from an array.

Always try to use native PHP functions, instead of rebuilding them with custom PHP code.

<?php

    $unique = array();
    foreach ($array as $b) {
        if (!in_array($b, $unique)) {
            /*  May be more code */
            $unique[] = $b;
        }
    }
?>

See also array_unique.

1.2.306.1. Suggestions

  • Turn the foreach() and its condition into a call to array_unique()

  • Extract the condition from the foreach() and add a separate call to array_unique()

1.2.306.2. Specs

Short name

Structures/CouldUseArrayUnique

Rulesets

All, Changed Behavior, Suggestions

Exakat since

1.2.6

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Examples

Dolibarr, OpenEMR

Available in

Entreprise Edition, Exakat Cloud