llms_assoc_array_insert( array $array, string $after_key, string $insert_key, mixed $insert_item )

Insert elements into an associative array after a specific array key

Description Description

If the requested key doesn’t exit, the new item will be added to the end of the array. If you need to insert at the beginning of an array use array_merge( $new_item, $orig_item ).

Top ↑

Parameters Parameters


(array) (Required) Original associative array.


(string) (Required) Key name in original array to insert new item after.


(string) (Required) Key name of the item to be inserted.


(mixed) (Required) Value to be inserted.

Top ↑

Return Return


Top ↑

Source Source

File: includes/llms.functions.core.php

function llms_assoc_array_insert( $array, $after_key, $insert_key, $insert_item ) {

	$res = array();

	$new_item = array(
		$insert_key => $insert_item,

	$index = array_search( $after_key, array_keys( $array ) );
	if ( false !== $index ) {

		$res = array_merge(
			array_slice( $array, 0, $index, true ),
			array_slice( $array, $index, count( $array ) - 1, true )
	} else {
		$res = array_merge( $array, $new_item );

	return $res;

Top ↑

Changelog Changelog

Version Description
3.21.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.