public static function extract(
        array $input,
        $keyIndex,
        $valueIndex,
        string $duplicateBehavior = 'takeLast'
    ) : array {
        if (!in_array($duplicateBehavior, ['takeFirst', 'takeLast', 'throw'])) {
            throw new \InvalidArgumentException("\$duplicateBehavior was not 'takeFirst', 'takeLast', or 'throw'");
        }

        self::ensureValidKey($keyIndex, '$keyIndex was not a string or integer');
        self::ensureValidKey($valueIndex, '$valueIndex was not a string or integer');

        $result = [];
        foreach ($input as $index => $array) {
            self::ensureIsArray($array, '$arrays was not a multi-dimensional array');

            $key = self::get($array, $keyIndex);
            self::ensureValidKey(
                $key,
                "Value for \$arrays[{$index}][{$keyIndex}] was not a string or integer",
                '\\UnexpectedValueException'
            );

            $value = self::get($array, $valueIndex);
            if (!array_key_exists($key, $result)) {
                $result[$key] = $value;
                continue;
            }

            if ($duplicateBehavior === 'throw') {
                throw new \Exception("Duplicate entry for '{$key}' found.");
            }

            if ($duplicateBehavior === 'takeLast') {
                $result[$key] = $value;
            }
        }

        return $result;
    }
public static function extract(
        array $input,
        $keyIndex,
        $valueIndex,
        string $duplicateBehavior = 'takeLast'
    ) : array {
        if (!in_array($duplicateBehavior, ['takeFirst', 'takeLast', 'throw'])) {
            throw new \InvalidArgumentException("\$duplicateBehavior was not 'takeFirst', 'takeLast', or 'throw'");
        }

        self::ensureValidKey($keyIndex, '$keyIndex was not a string or integer');
        self::ensureValidKey($valueIndex, '$valueIndex was not a string or integer');

        $result = [];
        foreach ($input as $index => $array) {
            self::ensureIsArray($array, '$arrays was not a multi-dimensional array');

            $key = self::get($array, $keyIndex);
            self::ensureValidKey(
                $key,
                "Value for \$arrays[{$index}][{$keyIndex}] was not a string or integer",
                '\\UnexpectedValueException'
            );

            $value = self::get($array, $valueIndex);
            if (!array_key_exists($key, $result)) {
                $result[$key] = $value;
                continue;
            }

            if ($duplicateBehavior === 'throw') {
                throw new \Exception("Duplicate entry for '{$key}' found.");
            }

            if ($duplicateBehavior === 'takeLast') {
                $result[$key] = $value;
            }
        }

        return $result;
    }
public static function extract ( array $ input , $ keyIndex , $ valueIndex , string $ duplicateBehavior = 'takeLast' ) : array { if ( ! in_array ( $ duplicateBehavior , [ 'takeFirst' , 'takeLast' , 'throw' ] ) ) { throw new \ InvalidArgumentException ( "\$duplicateBehavior was not 'takeFirst', 'takeLast', or 'throw'" ) ; } self :: ensureValidKey ( $ keyIndex , '$keyIndex was not a string or integer' ) ; self :: ensureValidKey ( $ valueIndex , '$valueIndex was not a string or integer' ) ; $ result = [ ] ; foreach ( $ input as $ index => $ array ) { self :: ensureIsArray ( $ array , '$arrays was not a multi-dimensional array' ) ; $ key = self :: get ( $ array , $ keyIndex ) ; self :: ensureValidKey ( $ key , "Value for \$arrays[{$index}][{$keyIndex}] was not a string or integer" , '\\UnexpectedValueException' ) ; $ value = self :: get ( $ array , $ valueIndex ) ; if ( ! array_key_exists ( $ key , $ result ) ) { $ result [ $ key ] = $ value ; continue ; } if ( $ duplicateBehavior === 'throw' ) { throw new \ Exception ( "Duplicate entry for '{$key}' found." ) ; } if ( $ duplicateBehavior === 'takeLast' ) { $ result [ $ key ] = $ value ; } } return $ result ; }
Extracts an associative array from the given multi-dimensional array.

@param array $input The multi-dimensional array.
@param string|int $keyIndex The index to be used as the key of the resulting single dimensional result array.
@param string|int $valueIndex The index to be used as the value of the resulting single dimensional result array.
If a sub array does not contain this element null will be used as the value.
@param string $duplicateBehavior Instruct how to handle duplicate resulting values, 'takeFirst', 'takeLast',
'throw'

@return array an associative array

@throws \InvalidArgumentException Thrown if $input is not an multi-dimensional array
@throws \InvalidArgumentException Thrown if $keyIndex is not an int or string
@throws \InvalidArgumentException Thrown if $valueIndex is not an int or string
@throws \InvalidArgumentException Thrown if $duplicateBehavior is not 'takeFirst', 'takeLast', 'throw'
@throws \UnexpectedValueException Thrown if a $keyIndex value is not a string or integer
@throws \Exception Thrown if $duplicatedBehavior is 'throw' and duplicate entries are found.
Extracts an associative array from the given multi - dimensional array .