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

PHP - Ds Sequence::join() Function



The Ds\Sequence::join() function is used to join all values together as a single string in a sequence.

This function accepts an optional parameter named 'glue', which specifies a string to insert between each value or separate each value with a specified glue. If this parameter is omitted, the values are joined ( or concatenated) without any separator.

Syntax

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

public abstract string Ds\Sequence::join([ string $glue ] )

Parameters

Following is the parameter of this function −

  • glue − An optional string to separate each value.

Return value

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

Example 1

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

<?php 
   $seq =  new \Ds\Vector([1, 2, 3, 4, 5]); 
   echo "The sequence values are: \n";
   print_r($seq);
   echo "The String after joining all values together: ";
   #using join() function
   var_dump($seq->join());
?>

Output

The above program produces the following output −

The sequence values are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The String after joining all values together: string(5) "12345"

Example 2

If the 'glue' parameter is omitted, all values are joined together without separating.

Following is another example of the PHPDs\Sequence::join()function. We use this function to join all values of this sequence (["Tutorials", "Point", 10, 20, 'a', 'b']) together as s a single string −

<?php 
   $seq =  new \Ds\Set(["Tutorials", "Point", 10, 20, 'a', 'b']); 
   echo "The sequence values are: \n";
   print_r($seq);
   echo "The String after joining all values together: \n";
   #using join() function
   var_dump($seq->join());
?>

Output

After executing the above program, the following output will be displayed −

The sequence values are:
Ds\Set Object
(
    [0] => Tutorials
    [1] => Point
    [2] => 10
    [3] => 20
    [4] => a
    [5] => b
)
The String after joining all values together:
string(20) "TutorialsPoint1020ab"

Example 3

If we pass an optional glue parameter to this function, it separates each value with the specified glue string −

<?php 
   $seq =  new \Ds\Set([10, 20, 30, 40, 50]); 
   echo "The sequence values are: \n";
   print_r($seq);
   $glue1 = "-";
   $glue2 = "%";
   echo "The glue1 and glue2 value is: ".$glue1.", ".$glue2;
   echo "\nThe String after joining all values together: \n";
   #using join() function
   var_dump($seq->join($glue1));
   var_dump($seq->join($glue2));
?>

Output

Once the above program is executed, it generates the following output −

The sequence values are:
Ds\Set Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The glue1 and glue2 value is: -, %
The String after joining all values together:
string(14) "10-20-30-40-50"
string(14) "10%20%30%40%50"
php_function_reference.htm
Advertisements