PHP - Ds Vector::join() 函式



PHP 的 Ds\Vector::join() 函式將向量的所有值連線成單個字串。此函式不會修改原始向量,並返回一個新的字串。

此函式接受一個可選引數作為分隔符。如果將引數傳遞給函式,則連線的值將由給定的分隔符分隔。如果沒有指定分隔符,則值將無需分隔符地連線。

語法

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

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

引數

此函式接受一個名為“glue”的單個引數,如下所述:

  • glue − 一個可選的分隔符,用於分隔連線的值。

返回值

此函式返回將向量的所有值連線在一起的字串。

示例 1

如果省略“glue”(例如分隔符)引數,則 PHP Ds\Vector::join() 函式將連線所有值,無需任何分隔符:

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The original vector: \n";   
   print_r($vector);
   echo("The vector elements after join(): ");
   #using join() function   
   print_r($vector->join());
?>

輸出

以上程式產生以下輸出:

The original vector:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The vector elements after join(): 12345

示例 2

如果我們將“glue”引數值設定為“-”,則值將連線在一起,並由給定的分隔符分隔。

以下是 PHP Ds\Vector::join() 函式的另一個示例。我們使用此函式使用指定的分隔符“-”將向量的所有值連線在一起:

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "Tutorix"]);
   echo "The original vector: \n";   
   print_r($vector);
   $glue = "-";
   echo "The separator is: '".$glue."'";
   echo "\nThe Vector elements after join(): ";
   #using join() function   
   print_r($vector->join($glue)); 
?>

輸出

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

The original vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
The separator is: '-'
The Vector elements after join(): Tutorials-Point-Tutorix

示例 3

在下面的示例中,我們使用join() 函式將向量的所有值連線在一起,並使用和不使用分隔符:

<?php 
   $vector = new \Ds\Vector(['a', 'e', 'i', 'o', 'u']);
   echo "The original vector: \n";   
   print_r($vector);
   $glue = "|";
   echo "\nThe Vector elements without separator: ";
   print_r($vector->join());
   echo "\nThe separator is: '".$glue."'";
   echo "\nThe Vector elements with separator: ";
   print_r($vector->join($glue)); 
?>

輸出

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

The original vector:
Ds\Vector Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)

The Vector elements without separator: aeiou
The separator is: '|'
The Vector elements with separator: a|e|i|o|u
php_function_reference.htm
廣告