Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Sequence::merge() Function



The PHP Ds\Sequence::merge() function is used to create a new sequence by adding the elements of the current sequence with the elements from traversable objects or arrays.

This function does not modify the original sequence. Instead, it returns a new sequence with the elements added in the same order as they were in the given traversable object or array.

Syntax

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

public abstract Ds\Sequence Ds\Sequence::merge( mixed $values )

Parameters

Following is the parameter of this function −

  • values − Traversable object or an array.

Return value

This function returns the result of adding all given values to a sequence.

Example 1

The following program demonstrates the usage of the PHP Ds\Sequence::merge() function −

<?php
   $seq = new \Ds\Vector([1, 2, 3]);
   $set = new \Ds\set([4, 5, 6]);
   echo "The original sequence is: \n";
   print_r($seq);
   echo "The given traversable object: \n";
   print_r($set);
   echo "The result after merge: \n";
   #using merge() function
   print_r($seq->merge($set));
?>

Output

Following is the output of the above program −

The original sequence is:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The given traversable object:
Ds\Set Object
(
    [0] => 4
    [1] => 5
    [2] => 6
)
The result after merge:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Example 2

Following is another example of the PHP Ds\Sequence::merge() function. We use this function to retrieve the result of adding all values to a sequence −

<?php
   $seq = new \Ds\Set(["Tutorials", "Point", "India"]);
   $vect = new \Ds\Vector(["Tutorix", "Hyderabad"]);
   echo "The original sequence is: \n";
   print_r($seq);
   echo "The given traversable object: \n";
   print_r($vect);
   echo "The result after merge: \n";
   #using merge() function
   print_r($seq->merge($vect));
?>

Output

After executing the above program, the following output is displayed −

The original sequence is:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The given traversable object:
Ds\Vector Object
(
    [0] => Tutorix
    [1] => Hyderabad
)
The result after merge:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
    [3] => Tutorix
    [4] => Hyderabad
)

Example 3

In the example below, we use the merge() function to create a new sequence by adding all values of current sequence with the given values −

<?php
   $seq = new \Ds\Set([10, 20]);
   echo "The original sequence is: \n";
   print_r($seq);
   $val1 = 'a';
   $val2 = 'b';
   $val3 = "Tutorials";
   $val4 = 2;
   echo "The given values are: ".$val1.", ".$val2.", ".$val3.", ".$val4;
   echo "\nThe result after merge: \n";
   #using merge() function
   print_r($seq->merge([$val1, $val2, $val3, $val4]));
?>

Output

The above program produces the following output −

The original sequence is:
Ds\Set Object
(
    [0] => 10
    [1] => 20
)
The given values are: a, b, Tutorials, 2
The result after merge:
Ds\Set Object
(
    [0] => 10
    [1] => 20
    [2] => a
    [3] => b
    [4] => Tutorials
    [5] => 2
)
php_function_reference.htm
Advertisements