1.2.380. Double array_flip()

Avoid double array_flip() to gain speed. While array_flip() alone is usually useful, a double call to array_flip() is made to make values and keys unique.

<?php

// without array_flip
function foo($array, $value) {
    $key = array_search($array, $value);

    if ($key !== false) {
        unset($array[$key]);
    }

    return $array;
}

// double array_flip
// array_flip() usage means that $array's values are all unique
function foo($array, $value) {
    $flipped = array_flip($value);
    unset($flipped[$value]);
    return array_flip($flipped);
}

?>

1.2.380.1. Suggestions

  • Use array_unique() or array_count_values().

  • Use array_flip() once, and let PHP garbage collect it later.

  • Keep the original values in a separate variable.

1.2.380.2. Specs

Short name

Performances/DoubleArrayFlip

Rulesets

All, Changed Behavior, Performances

Exakat since

1.1.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Examples

NextCloud

Available in

Entreprise Edition, Exakat Cloud