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 ).
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.
Return Return
(array)
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;
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
3.21.0 | Introduced. |