wpcasa icon indicating copy to clipboard operation
wpcasa copied to clipboard

Add helper function wpsight_array_insert_after()

Open simonrimkus opened this issue 8 years ago • 0 comments

/**
 *  wpsight_array_insert_after()
 *
 *  Insert a value or key/value pair after a specific key in an array. If key doesn't exist, value is appended
 *  to the end of the array.
 *  
 *  @param  array   $array
 *  @param  string  $key
 *  @param  array   $new    
 *  @return array
 *
 *  @see    https://gist.github.com/wpscholar/0deadce1bbfa4adb4e4c
 */
function wpsight_array_insert_after( array $array, $key, array $new ) {
    $keys = array_keys( $array );
    $index = array_search( $key, $keys );
    $pos = false === $index ? count( $array ) : $index + 1;

    return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
}

simonrimkus avatar Jun 26 '16 09:06 simonrimkus