当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP Ds\Sequence rotate()用法及代码示例


Ds \ Sequence::rotate()函数是PHP中的一个内置函数,用于将序列元素旋转给定的旋转数。

用法:

void abstract public Ds\Sequence::rotate ( int $rotations )

参数:该函数接受单个参数$rotations,该参数保存转数。


返回值:该函数不返回任何值。

以下示例程序旨在说明PHP中的Ds \ Sequence::rotate()函数:

程序1:

<?php  
  
// Create new Sequence 
$seq = new \Ds\Vector([1, 2, 3, 4, 5]);  
  
echo("Original Sequence\n");  
  
// Display the Sequence elements  
print_r($seq);  
  
// Use rotate() function to rotate  
// the sequence elements  
$seq->rotate(3);  
  
echo("\nSequence after rotating by 3 places\n");  
  
// Display the Sequence elements  
print_r($seq);  
  
?> 
输出:
Original Sequence
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Sequence after rotating by 3 places
Ds\Vector Object
(
    [0] => 4
    [1] => 5
    [2] => 1
    [3] => 2
    [4] => 3
)

程序2:

<?php  
  
// Create new Sequence 
$seq = new \Ds\Vector(["Geeks", "for", "Geeks",  
        "Computer", "Science", "Portal"]);  
  
echo("Original Sequence\n");  
  
// Display the Sequence elements  
print_r($seq);  
  
// Use rotate() function to rotate  
// the sequence elements  
$seq->rotate(8);  
  
echo("\nSequence after rotating by 8 places\n");  
  
// Display the Sequence elements  
print_r($seq);  
  
?> 
输出:
Original Sequence
Ds\Vector Object
(
    [0] => Geeks
    [1] => for
    [2] => Geeks
    [3] => Computer
    [4] => Science
    [5] => Portal
)

Sequence after rotating by 8 places
Ds\Vector Object
(
    [0] => Geeks
    [1] => Computer
    [2] => Science
    [3] => Portal
    [4] => Geeks
    [5] => for
)

参考: https://www.php.net/manual/en/ds-sequence.rotate.php



相关用法


注:本文由纯净天空筛选整理自R_Raj大神的英文原创作品 PHP | Ds\Sequence rotate() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。