RAD function: insert

A RAD function that returns an array with one or more inserted elements.

Function

insert

Format

insert( $array[, $position[, $number[, $value[, $denull]]]])
Parameters
  • The $array parameter is the array into which an element is inserted.
  • The $position parameter is the position of the first inserted element. If the value is zero (0), the element is inserted at the end of the array. This is an optional parameter. The default value is zero (0).
  • The $number parameter is the number of elements to insert. This is an optional parameter. The default value is one (1). If the value is zero (0), this function will return without changing the array.
  • The $value parameter is a value to insert into the new elements. This is an optional parameter. The default value is NULL.
  • The $denull parameter is a logical value that determines whether or not the array should be denulled before inserting elements. This is an optional parameter. The default value is true.

Factors

If the source array has NULL elements at the end, this function automatically denulls the array before inserting new elements in any position. To avoid this, set the value of the $denull parameter to false.

Examples

The following examples show a desired result and the syntax used to achieve that result.

Action desired Example and result
Insert an element at the beginning of an array.
$a={"a", "b", "c", "d"}
$a=insert($a, 1, 1, "z")

Where:

The value of $a is {"z", "a", "b", "c", "d"}

Insert an element in the middle of an array.

To insert an element in the middle of an array:

$a={"a", "b", "c", "d"}
$a=insert($a, 3, 1, "z")

Where:

The value of $a is {"a", "b", "z", "c", "d"}

Insert an element at the end of an array.
$a={"a", "b", "c", "d"}
$a=insert($a, 0, 1, "z")

Where:

The value of $a is {"a", "b", "c", "d", "z"}

The array is denulled.
$a={"a", "b",  ,  , "e", "f",  ,  ,  , }
$a=insert( $a, 0, 1, "z")

Where:

The value of $a is {"a", "b", , , "e", "f", "z"}

To avoid denulling the array.
$a={"a", "b",  ,  , "e", "f",  ,  ,  , }
$a=insert( $a, 0, 1, "z", 0)

Where:

The value of $a is {"a", "b", , , "e", "f", , , , ,"z"}

The array is (again) denulled.
 $a={"a", "b",  ,  , "e", "f",  ,  ,  , }
$a=insert( $a, 2, 1, "z")

Where:

The value of $a is {"a", "z", "b", , , "e", "f"}