ap_array_insert_after( array $arr = array(), string $key = '', array $is_new = array() )

Description #

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.

Parameters #

  • $arr
    array (Optional) Array. Default value: array()
  • $key
    string (Optional) Key. Default value: ''
  • $is_new
    array (Optional) Array. Default value: array()

Source #

File: includes/functions.php

function ap_array_insert_after( $arr = array(), $key = '', $is_new = array() ) {
	$keys  = array_keys( $arr );
	$index = array_search( $key, $keys ); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
	$pos   = false === $index ? count( $arr ) : $index + 1;

	return array_merge( array_slice( $arr, 0, $pos ), $is_new, array_slice( $arr, $pos ) );
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment