Can only flip string and integer values, entry skipped

Description

array_flip() converts values into keys and keys into values. The former values undergo a process to convert them to valid keys, which are strings or integers.

In general, non-scalar values cannot be converted, and are skipped: this is the case of arrays.

In particular, string or integer backed enumerations and stringable objects are not converted to strings. This applies with or without strict_types.

Example

<?php

array_flip([[1], E::A, new X])

enum E : string {
    case A = 'a';
}

class X implements Stringable {
    function __toString() {
        return 'X';
    }
}


?>

Solutions

  • Force the type to be string when building the array.

  • Apply a converting method with array_map(), before calling array_filter.