PHP | Ds\Set merge() Function

The Ds\Set::merge() function is an inbuilt function in PHP which returns a set after adding all given values to the set.

Syntax:

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

Parameters: This function accepts single parameter $values which holds the elements.



Return Value: This function returns the set adding all the elements.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Create new set
$set = new \Ds\Set([12, 15, 18, 20]); 
  
// Merge the set element and display it 
var_dump($set->merge([1, 2, 3])); 
  
// Display the set element 
var_dump($set
?> 

chevron_right


Output:

object(Ds\Set)#2 (7) {
  [0]=>
  int(12)
  [1]=>
  int(15)
  [2]=>
  int(18)
  [3]=>
  int(20)
  [4]=>
  int(1)
  [5]=>
  int(2)
  [6]=>
  int(3)
}
object(Ds\Set)#1 (4) {
  [0]=>
  int(12)
  [1]=>
  int(15)
  [2]=>
  int(18)
  [3]=>
  int(20)
}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Create new set
$set = new \Ds\Set([12, 15, 18, 20]); 
  
// Merge the set element and display it 
var_dump($set->merge(["G", "E", "E", "k", "S"])); 
  
?> 

chevron_right


Output:

object(Ds\Set)#2 (8) {
  [0]=>
  int(12)
  [1]=>
  int(15)
  [2]=>
  int(18)
  [3]=>
  int(20)
  [4]=>
  string(1) "G"
  [5]=>
  string(1) "E"
  [6]=>
  string(1) "k"
  [7]=>
  string(1) "S"
}

Reference: https://www.php.net/manual/en/ds-set.merge.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.