Related Tags

php

What is the DS\Vector insert() method in PHP?

Talha Ashar

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The insert() method of the DS\Vector class in PHP inserts values into a vector at a specified index.

The process is illustrated below:

The prototype of the insert() method is shown below:

void public Ds\Vector::insert( int ＄index, mixed ...＄values )

Parameters

The insert() method accepts the following parameters:

• index: This parameter specifies the index of the vector at which the values are to be inserted. The index parameter should be an integer between zero and the number of values in the vector. If index is outside the valid range of values, the insert() method throws the OutOfRangeException.

• values: This parameter specifies the values to insert into the vector. You can insert any number of values and they can be of different data types.

Return value

The insert() method does not return a value.

Example

The code below shows how the insert() method works in PHP:

<?php
$firstVector = new \Ds\Vector([1, 2, 3]);$secondVector = new \Ds\Vector();
// insert value at index 1
$firstVector->insert(1, 10); print_r($firstVector);
print("\n");
// insert multiple values at index 3
$firstVector->insert(3, "abc", 20, "def"); print_r($firstVector);
print("\n");
// insert value at index 0
$secondVector->insert(0, 5); print_r($secondVector);
print("\n");
// insert value at index 1
$secondVector->insert(1, "efg"); print_r($secondVector);
?>

The code above produces the following output:

Ds\Vector Object
(
[0] => 1
[1] => 10
[2] => 2
[3] => 3
)

Ds\Vector Object
(
[0] => 1
[1] => 10
[2] => 2
[3] => abc
[4] => 20
[5] => def
[6] => 3
)

Ds\Vector Object
(
[0] => 5
)

Ds\Vector Object
(
[0] => 5
[1] => efg
)

Note: To run this program, you need to install Data Structures for PHP on your local machine.

Explanation

First, two vectors are initialized: firstVector and secondVector. firstVector contains only integer values, whereas secondVector is an empty vector.

The insert() method in line $6$ inserts the value 10 at index 1 of firstVector. Before the insertion, the value stored at index 1 of firstVector is 2, so the insert() method shifts all values at index 1 and beyond by one place to the right, and inserts 10 at index 1.

The insert() method in line $12$ works similarly, except that it inserts three values instead of one. All three values, abc, ＄20, and def, are inserted starting at index 3.

Since secondVector is an empty vector, the only valid index where a value can be inserted is index 0, i.e., the first index of the vector. The insert() method in line $18$ inserts the value 5 at the start of secondVector.

Finally, the insert() method in line $24$ inserts the value efg at secondVector's tail. Although secondVector contains only one value, the insert() method accepts index 1 as a valid argument because any insertion to the vector would involve index 1 getting a value. However, if an index greater than $1$ was provided as an argument, the insert() function would have thrown the OutOfRangeException.

RELATED TAGS

php

CONTRIBUTOR

Talha Ashar