PHP - Ds Vector::push() Function



The PHP Ds\Vector::push() function is used to add the specified values to the end of a vector. This function allows you to add multiple values to a vector at once by specifying them together.

The Ds\Vector class also provides a function named insert() to insert elements at a specified index. If you specify the index as the position of the last element, the element will be appended at the end.

Syntax

Following is another example of the PHP Ds\Vector::push() function −

public Ds\Vector::push(mixed ...$values): void

Parameters

This function accepts the following parameters −

  • values − The single or multiple values need to be inserted.

Return value

This function doesn't return any value.

Example 1

The following is the basic example of the PHP Ds\Vector::push() function −

<?php 
   $vector = new \Ds\Vector([10, 20, 30, 40]); 
   echo("The original vector elements: \n"); 
   print_r($vector);
   $value = 50;
   echo "The given value is: ".$value;
   #using push() function
   $vector->push($value);
   echo("\nAfter appending the elements to vector: \n"); 
   print_r($vector); 
?>

Output

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

The original vector elements:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)
The given value is: 50
After appending the elements to vector:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Example 2

Following is another example of the PHP Ds\Vector::push() function. We use this function to add the value "India" at the end of this vector (["Tutorials", "Point"]) −

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point"]); 
   echo("The original vector elements: \n"); 
   print_r($vector);
   $value = "India";
   echo "The given value is: ".$value;
   #using push() function
   $vector->push($value); 
   echo("\nAfter appending the elements to vector: \n"); 
   print_r($vector);
?> 

Output

The above program produces the following output −

The original vector elements:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
)
The given value is: India
After appending the elements to vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)

Example 3

Adding multiple elements at the end of this vector at once.

In the example below, we use the push() function to add multiple values 'c', 'd', 'e', and 'f' at the end of this vector (['a', 'b']) at once −

<?php 
   $vector = new \Ds\Vector(['a', 'b']); 
   echo("The original vector elements: \n"); 
   print_r($vector);
   $v1 = 'c';
   $v2 = 'd';
   $v3 = 'e';
   $v4 = 'f';
   echo "The given values are: ".$v1.", ".$v2.", ".$v3.", ".$v4;
   #using push() function
   $vector->push($v1, $v2, $v3, $v4);
   echo("\nAfter appending the elements to vector: \n"); 
   print_r($vector); 
?>

Output

On executing the above program, it will generate the following output −

The original vector elements:
Ds\Vector Object
(
    [0] => a
    [1] => b
)
The given values are: c, d, e, f
After appending the elements to vector:
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)
php_function_reference.htm
Advertisements