當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP Ds\Sequence reversed()用法及代碼示例


Ds \ Sequence::reversed()函數是PHP中的一個內置函數,用於返回序列元素的反向副本。

用法:

Ds\Sequence abstract public Ds\Sequence::reversed ( void )

參數:該函數不接受任何參數。


返回值:此函數返回序列元素的反向副本。

以下示例程序旨在說明PHP中的Ds \ Sequence::reversed()函數:

程序1:

<?php  
  
// Create new Sequence  
$seq = new \Ds\Vector([1, 2, 3, 4, 5]);  
  
// Display the sequence elements  
print_r($seq);  
  
echo("After reversing the sequence elements\n");  
  
// Use reversed() function to reverse  
// the copy of sequence and display it  
print_r($seq->reversed());  
  
?> 
輸出:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
After reversing the sequence elements
Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

程序2:

<?php  
  
// Create new Vector  
$seq = new \Ds\Vector(["Geeks", "for", "Geeks", 
        "Computer", "Science", "Portal"]);  
  
// Display the elements  
print_r($seq);  
  
echo("Sequence after reversing\n");  
  
// Use reversed() function to reverse  
// the element and display it  
print_r($seq->reversed()); 
  
?>
輸出:
Ds\Vector Object
(
    [0] => Geeks
    [1] => for
    [2] => Geeks
    [3] => Computer
    [4] => Science
    [5] => Portal
)
Sequence after reversing
Ds\Vector Object
(
    [0] => Portal
    [1] => Science
    [2] => Computer
    [3] => Geeks
    [4] => for
    [5] => Geeks
)

參考: https://www.php.net/manual/en/ds-sequence.reversed.php



相關用法


注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | Ds\Sequence reversed() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。