src/Utilities/Arrays.php
public static function setKeysAsValues(array $array, $ignoreDuplicatedValues = true)
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        $replaced = [];

        foreach ($array as $key => $value) {
            /*
             * The value it's an array?
             * Let's replace keys with values in this array first
             */
            if (is_array($value)) {
                $replaced[$key] = self::setKeysAsValues($value, $ignoreDuplicatedValues);

                continue;
            }

            // Duplicated values shouldn't be ignored and processed value is used as key already?
            // Let's use an array and that will contain all values (to avoid ignoring / overriding duplicated values)
            if (!$ignoreDuplicatedValues && isset($replaced[$value])) {
                $existing = self::makeArray($replaced[$value]);

                $replaced[$value] = array_merge($existing, [
                    $key,
                ]);

                continue;
            }

            // Standard behaviour
            $replaced[$value] = $key;
        }

        return $replaced;
    }
public static function setKeysAsValues(array $array, $ignoreDuplicatedValues = true)
    {
        /*
         * No elements?
         * Nothing to do
         */
        if (empty($array)) {
            return null;
        }

        $replaced = [];

        foreach ($array as $key => $value) {
            /*
             * The value it's an array?
             * Let's replace keys with values in this array first
             */
            if (is_array($value)) {
                $replaced[$key] = self::setKeysAsValues($value, $ignoreDuplicatedValues);

                continue;
            }

            // Duplicated values shouldn't be ignored and processed value is used as key already?
            // Let's use an array and that will contain all values (to avoid ignoring / overriding duplicated values)
            if (!$ignoreDuplicatedValues && isset($replaced[$value])) {
                $existing = self::makeArray($replaced[$value]);

                $replaced[$value] = array_merge($existing, [
                    $key,
                ]);

                continue;
            }

            // Standard behaviour
            $replaced[$value] = $key;
        }

        return $replaced;
    }
public static function setKeysAsValues ( array $ array , $ ignoreDuplicatedValues = true ) { /*
         * No elements?
         * Nothing to do
         */ if ( empty ( $ array ) ) { return null ; } $ replaced = [ ] ; foreach ( $ array as $ key => $ value ) { /*
             * The value it's an array?
             * Let's replace keys with values in this array first
             */ if ( is_array ( $ value ) ) { $ replaced [ $ key ] = self :: setKeysAsValues ( $ value , $ ignoreDuplicatedValues ) ; continue ; } // Duplicated values shouldn't be ignored and processed value is used as key already? // Let's use an array and that will contain all values (to avoid ignoring / overriding duplicated values) if ( ! $ ignoreDuplicatedValues && isset ( $ replaced [ $ value ] ) ) { $ existing = self :: makeArray ( $ replaced [ $ value ] ) ; $ replaced [ $ value ] = array_merge ( $ existing , [ $ key , ] ) ; continue ; } // Standard behaviour $ replaced [ $ value ] = $ key ; } return $ replaced ; }
Sets keys as values and values as keys in given array.
Replaces keys with values.

@param array $array                  The array to change values with keys
@param bool  $ignoreDuplicatedValues (optional) If is set to true, duplicated values are ignored. This means that
when there is more than 1 value and that values become key, only the last
value will be used with it's key, because other will be overridden.
Otherwise - values are preserved and keys assigned to that values are
returned as an array.
@return null|array

Example of $ignoreDuplicatedValues = false:
- provided array
$array = [
'lorem' => 100,     // <-- Duplicated value
'ipsum' => 200,
'dolor' => 100,     // <-- Duplicated value
];

- result
$replaced = [
100 => [
'lorem',        // <-- Key of duplicated value
'dolor',        // <-- Key of duplicated value
],
200 => 'ipsum',
];
Sets keys as values and values as keys in given array . Replaces keys with values .