protected function insertInArray($array, $key, $value, $beforeKey)
    {
        // Find the position of the array
        $position = array_search($beforeKey, array_keys($array));

        // Safeguard: silently append if injected position could not be found
        if (false === $position) {
            // @codeCoverageIgnoreStart
            $array[ $key ] = $value;
            return $array;
            // @codeCoverageIgnoreEnd
        }

        if (0 === $position) {
            return [ $key => $value ] + $array;
        }

        // Slice the array up with the new entry in between
        return array_slice($array, 0, $position, true)
             + [ $key => $value ]
             + array_slice($array, $position, count($array) - $position, true);
    }
protected function insertInArray($array, $key, $value, $beforeKey)
    {
        // Find the position of the array
        $position = array_search($beforeKey, array_keys($array));

        // Safeguard: silently append if injected position could not be found
        if (false === $position) {
            // @codeCoverageIgnoreStart
            $array[ $key ] = $value;
            return $array;
            // @codeCoverageIgnoreEnd
        }

        if (0 === $position) {
            return [ $key => $value ] + $array;
        }

        // Slice the array up with the new entry in between
        return array_slice($array, 0, $position, true)
             + [ $key => $value ]
             + array_slice($array, $position, count($array) - $position, true);
    }
protected function insertInArray ( $ array , $ key , $ value , $ beforeKey ) { // Find the position of the array $ position = array_search ( $ beforeKey , array_keys ( $ array ) ) ; // Safeguard: silently append if injected position could not be found if ( false === $ position ) { // @codeCoverageIgnoreStart $ array [ $ key ] = $ value ; return $ array ; // @codeCoverageIgnoreEnd } if ( 0 === $ position ) { return [ $ key => $ value ] + $ array ; } // Slice the array up with the new entry in between return array_slice ( $ array , 0 , $ position , true ) + [ $ key => $ value ] + array_slice ( $ array , $ position , count ( $ array ) - $ position , true ) ; }
Insert an item into an associative array at the position before a given key.

@param array  $array
@param string $key
@param mixed  $value
@param string $beforeKey
@return array
Insert an item into an associative array at the position before a given key .