• PHP Video Tutorials

PHP - Ds Set::join() Function



The PHP Ds\Set::join() function is used to join all values as a string of the current set. If this set contains duplicate values, they will be joined only once, and the rest of the duplicate values will be ignored.

For example, consider a set containing the values ["h", "e", "l", "l", "o"]. If we try to join all values of this set using the join() function, the value "l" will be ignored and joined only once, and the output will be "helo".

You can use an optional parameter to separate the joined values of the current set by specifying the glue parameter value such as ",", "|", "$", "-", etc.

Syntax

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

public Ds\Set::join(string $glue = ?): string

Parameters

This function accepts an optional parameter named 'glue', which is described below −

  • $glue − An optional string to separate each value.

Return value

This function returns all values of a set joined together as a string.

Example 1

The following is the basic example of the PHP Ds\Set::join() function −

<?php
   $set = new \Ds\Set(["I", "N", "D", "I", "A", 1, 2, 3, 4]);
   echo "Set elements before joining: \n";
   print_r($set);
   echo "Set elements after joining: \n";
   #using join() function
   var_dump($set->join());  
?> 

Output

The above program produces the following output −

Set elements before joining:
Ds\Set Object
(
    [0] => I
    [1] => N
    [2] => D
    [3] => A
    [4] => 1
    [5] => 2
    [6] => 3
    [7] => 4
)
Set elements after joining:
string(8) "INDA1234"

Example 2

The following is another example of the PHP Ds\Set::join() function. We use this function to join all values of this set (["T", "u", "t", "o", "r", "i", "a", "l", "s"]) together with comma-separated (,) −

<?php  
   $set = new \Ds\Set(["T", "u", "t", "o", "r", "i", "a", "l", "s"]);
   echo "The set elements before joining: \n";
   print_r($set);
   $glue =",";
   echo "The glue value: (" . $glue. ")\n";
   echo "The set elements after joining with comma(,) separated: \n";
   #using join() function
   var_dump($set->join($glue));
?>

Output

After executing the above program, it will display the following output −

The set elements before joining:
Ds\Set Object
(
    [0] => T
    [1] => u
    [2] => t
    [3] => o
    [4] => r
    [5] => i
    [6] => a
    [7] => l
    [8] => s
)
The glue value: (,)
The set elements after joining with comma(,) separated:
string(25) "T, u, t, o, r, i, a, l, s"

Example 3

If the current set contains duplicate values, then they will be joined once, and the rest will be ignored while joining values together −

<?php  
   $set = new \Ds\Set(["T", "u", "t", "o", "r", "i", "a", "l", "s", "p", "o", "i", "n", "t"]);
   echo "The set elements before joining: \n";
   print_r($set);
   $glue ="|";
   echo "The glue value: (" . $glue. ")\n";
   echo "The set elements after joining with comma(|) separated: \n";
   #using join() function
   var_dump($set->join($glue));
?>

Output

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

The set elements before joining:
Ds\Set Object
(
    [0] => T
    [1] => u
    [2] => t
    [3] => o
    [4] => r
    [5] => i
    [6] => a
    [7] => l
    [8] => s
    [9] => p
    [10] => n
)
The glue value: (|)
The set elements after joining with comma(|) separated:
string(21) "T|u|t|o|r|i|a|l|s|p|n"
php_function_reference.htm
Advertisements