PHP - Ds Sequence::join() 函式



Ds\Sequence::join() 函式用於將序列中的所有值連線成單個字串。

此函式接受一個名為 'glue' 的可選引數,該引數指定要在每個值之間插入的字串,或者用指定的粘合劑分隔每個值。如果省略此引數,則值將連線(或連線)而沒有任何分隔符。

語法

以下是 PHP Ds\Sequence::join() 函式的語法:

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

引數

以下是此函式的引數:

  • glue − 一個可選的字串,用於分隔每個值。

返回值

此函式返回將序列的所有值連線在一起形成的字串。

示例 1

以下是 PHP Ds\Sequence::join() 函式的基本示例:

<?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());
?>

輸出

以上程式產生以下輸出:

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"

示例 2

如果省略 'glue' 引數,則所有值將連線在一起而無需分隔。

以下是 PHP Ds\Sequence::join() 函式的另一個示例。我們使用此函式將此序列(["Tutorials", "Point", 10, 20, 'a', 'b'])的所有值連線在一起形成單個字串:

<?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());
?>

輸出

執行上述程式後,將顯示以下輸出:

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"

示例 3

如果我們將一個可選的 glue 引數傳遞給此函式,它將用指定的粘合劑字串分隔每個值:

<?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));
?>

輸出

執行上述程式後,它會生成以下輸出:

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
廣告