
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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 )