• PHP Video Tutorials

PHP - Ds Set::merge() Function



The PHP Ds\Set::merge() function retrieves the result of adding all given values to a current set. This function does not affect the current instance.

Which means, that if you add values to the current set and try to display the element of the set after adding, it will display the original set values.

To display a set that includes all the given elements, you need to create a new set and store the result of the merge() function. Then, you can display the new set that includes all the newly added values.

Syntax

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

public Ds\Set::merge(mixed $values): Ds\Set

Parameters

Following is the parameter of this function −

  • values − A traversal object or an array (or values needs to be added).

Return value

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

Example 1

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

<?php  
   $set = new \Ds\Set([1, 2, 3]);
   echo "The original set values are: \n";
   print_r($set);
   $obj = ([4, 5, 6]);
   echo "The values needs to be added: \n";
   print_r($obj); 
   $new_set = $set->merge($obj);
   echo "The set values after adding: \n";
   print_r($new_set);
?> 

Output

After executing the above program, it displays the following output −

The original set values are:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
The values needs to be added:
Array
(
    [0] => 4
    [1] => 5
    [2] => 6
)
The set values after adding:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Example 2

Following is another example of the PHP Ds\Set::merge() function. We use this function to retrieve the result of adding all the specified array or traversal object values ["o", "u"] to this set (["a", "e", "i"]) −

<?php  
   $set = new \Ds\Set(["a", "e", "i"]);
   echo "The original set values are: \n";
   print_r($set);
   $obj = (["o", "u"]);
   echo "Values needs to be added: \n";
   print_r($obj);
   echo "The set after adding values: \n";
   #using merge() function 
   $new_set = $set->merge(["o", "u"]);
   print_r($new_set);
?>  

Output

The above program produces the following output −

The original set values are:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
)
Values needs to be added:
Array
(
    [0] => o
    [1] => u
)
The set after adding values:
Ds\Set Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)

Example 3

As we discussed earlier the current instance will not be affected, so if we try to display the original set values, it will display the same as before adding it was.

<?php  
   $set = new \Ds\Set([10, 20]);
   echo "The original set values are: \n";
   print_r($set);
   $obj = ([30, 40]);
   echo "Values needs to be added: \n";
   print_r($obj);
   echo "The set after adding values: \n";
   #using merge() function 
   $set->merge(["o", "u"]);
   print_r($set);
?>

Output

Once the above program is executed, it will generate the following output −

The original set values are:
Ds\Set Object
(
    [0] => 10
    [1] => 20
)
Values needs to be added:
Array
(
    [0] => 30
    [1] => 40
)
The set after adding values:
Ds\Set Object
(
    [0] => 10
    [1] => 20
)
php_function_reference.htm
Advertisements