PHP - Ds Sequence::insert() Function



The PHPDs\Sequence::insert()function is used to insert values at a given index in a sequence. The 'index' is the position of elements in the sequence, which starts at 0, represents the first element, 1 represents the second element, and so on.

If the specified index is "invalid" (i.e., negative or larger than the sequence size), this function will throw an "OutOfRangeException".

Syntax

Following is the syntax of the PHP Ds\Sequence::insert() function −

abstract public Ds\Sequence::insert(int $index, mixed ...$values): void

Parameters

Following are the parameters of this function −

  • index − An index at which the values need to be inserted.
  • values − Single or multiple values to insert.

Return value

This function does not return any value.

Example 1

The following is the basic example of the PHP Ds\Sequence::insert() function −

<?php
   $seq = new \Ds\Vector([2, 3, 4, 5]);
   echo "The original sequence: \n";
   print_r($seq);
   $index = 0;
   $val = 1;
   echo "The given index is: ".$index;
   echo "\nThe given value is: ".$val;
   echo "\nThe sequence after insert: \n";
   #using insert() function
   $seq->insert($index, $val);
   print_r($seq);
?>

Output

The above program produces the following output −

The original sequence:
Ds\Vector Object
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
)
The given index is: 0
The given value is: 1
The sequence after insert:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example 2

Following is another example of the PHP Ds\Sequence::insert() function. We use this function to insert values at the specified indices in this sequence ([])

<?php
   $seq = new \Ds\Vector([]);
   echo "The original sequence: \n";
   print_r($seq);
   $count = 0;
   for($i = 0; $i<=10; $i++){
	   $count += 1;
	   #using insert() function
	   $seq->insert($i, $count);
   }
   echo "The sequence after insert: \n";
   print_r($seq);
?>

Output

After executing the above program, the following output will be displayed −

The original sequence:
Ds\Vector Object
(
)
The sequence after insert:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
)

Example 3

If the given index is invalid (i.e, negative), the insert() function will throw an "OutOfRangeException" as follows −

<?php
   $seq = new \Ds\Vector(['a', 'b', 'c']);
   echo "The original sequence: \n";
   print_r($seq);
   $index = -1;
   $val = 'a';
   echo "The given index is: ".$index;
   echo "\nThe given value is: ".$val;
   echo "\nThe sequence after insert: \n";
   #using insert() function
   $seq->insert($index, $val);
   print_r($seq);
?>

Output

Once the above program is executed, it generates the following output −

The original sequence:
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
)
The given index is: -1
The given value is: a
The sequence after insert:
PHP Fatal error:  Uncaught OutOfRangeException: 
Index out of range: -1, expected 0 <= x <= 3 in C:\Apache24\htdocs\index.php:11
Stack trace:
#0 C:\Apache24\htdocs\index.php(11): Ds\Vector->insert(-1, 'a')
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 11
php_function_reference.htm
Advertisements