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

(array) (Required) Original associative array.

$after_key

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

$insert_key

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

$insert_item

(mixed) (Required) Value to be inserted.


Top ↑

Return Return

(array)


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 ) {
		$index++;

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

	return $res;



Top ↑

Changelog 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.