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


PHP Ds\Deque merge()用法及代码示例


Ds \ Deque::merge()函数是PHP中的一个内置函数,用于通过将所有Deque的所有元素合并到一个副本中并将所有值添加到副本中来返回合并的Deque,然后返回该副本。

用法:

public Ds\Deque::merge( $values ):Ds\Deque

参数:此函数接受单个参数$values,其中包含要与调用Deque合并的值。


返回值:此函数返回一个双端队列,其中包含两个双端队列的所有元素。

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

程序1:

<?php 
  
// Declare a deque 
$deck = new \Ds\Deque([10, 20, 30, 40, 50, 60]); 
  
echo("Elements of first deque\n"); 
  
// Display the deque Elements 
print_r($deck); 
  
// Declare another deque 
$deck2 = new \Ds\Deque([70, 80, 90, 100]); 
  
echo("\nElements of second deque\n"); 
print_r($deck2); 
  
echo("\nMerged deque elements\n"); 
  
// Merge the both deque 
print_r($deck->merge($deck2)); 
  
?>
输出:
Elements of first deque
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

Elements of second deque
Ds\Deque Object
(
    [0] => 70
    [1] => 80
    [2] => 90
    [3] => 100
)

Merged deque elements
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
    [6] => 70
    [7] => 80
    [8] => 90
    [9] => 100
)

程序2:

<?php 
  
// Declare a deque 
$deck = new \Ds\Deque(["geeks", "for", "geeks"]); 
  
echo("Elements of first deque\n"); 
  
// Display the deque Elements 
print_r($deck); 
  
// Declare another deque 
$deck2 = new \Ds\Deque(["practicing", "data", "structures"]); 
  
echo("\nElements of second deque\n"); 
print_r($deck2); 
  
echo("\nMerged deque elements\n"); 
  
// Merge the both deque 
print_r($deck->merge($deck2)); 
  
?>
输出:
Elements of first deque
Ds\Deque Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
)

Elements of second deque
Ds\Deque Object
(
    [0] => practicing
    [1] => data
    [2] => structures
)

Merged deque elements
Ds\Deque Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
    [3] => practicing
    [4] => data
    [5] => structures
)

参考: http://php.net/manual/en/ds-deque.merge.php



相关用法


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