Wednesday, June 10, 2026
HomeLanguagesPHP | DsVector merge() Function

PHP | Ds\Vector merge() Function

The Ds\Vector::merge() function is an inbuilt function in PHP which is used to merge all the elements to the vector.

Syntax: 

Ds\Vector public Ds\Vector::merge( $values )

Parameters: This function accepts a single parameter $values which is the traversable object or array.
Return Value: This function returns the copy of the vector after merging the elements to the vector.

Below programs illustrate the Ds\Vector::merge() function in PHP:

Program 1:  

PHP




<?php
 
// Create new vector
$arr1 = new \Ds\Vector([10, 20, 30, 40, 50]);
 
echo("Original vector elements\n");
print_r($arr1);
 
// Create new vector
$arr2 = new \Ds\Vector([60, 70, 80, 90, 100]);
 
echo("Vector Elements after merging\n");
 
// Use merge() function to merge two vector
// and display its resultant vector
print_r($arr1->merge($arr2));
 
?>


Output: 

Original vector elements
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
Vector Elements after merging
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
    [6] => 70
    [7] => 80
    [8] => 90
    [9] => 100
)

Program 2: 

PHP




<?php
 
// Create new vector
$arr1 = new \Ds\Vector(["neveropen", "for", "neveropen"]);
 
echo("Original vector elements\n");
print_r($arr1);
 
// Create new vector
$arr2 = new \Ds\Vector([60, 70, 100]);
 
echo("Vector Elements after merging\n");
 
// Use merge() function to merge two vector
// and display its resultant vector
print_r($arr1->merge($arr2));
 
?>


Output: 

Original vector elements
Ds\Vector Object
(
    [0] => neveropen
    [1] => for
    [2] => neveropen
)
Vector Elements after merging
Ds\Vector Object
(
    [0] => neveropen
    [1] => for
    [2] => neveropen
    [3] => 60
    [4] => 70
    [5] => 100
)

Reference: http://php.net/manual/en/ds-vector.merge.php
 

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS