PHP - Ds Vector::merge() 函数



PHP 的 Ds\Sequence::merge() 函数用于检索将所有给定值添加到序列的结果。此函数将所有元素合并到一个向量中,并在合并所有值后返回向量的副本。

例如,v1 = ([1, 2]),v2 = ([3, 4]),则结果向量将为 = ([1, 2, 3, 4])。

语法

以下是 PHP Ds\Sequence::merge() 函数的语法:

public Ds\Vector::merge(mixed $values): Ds\Vector

参数

以下是此函数的参数:

  • $values - 需要合并的遍历对象或数组。

返回值

此函数返回将所有给定值添加到序列的结果。

示例 1

以下程序演示了 PHP Ds\Sequence::merge() 函数的使用:

<?php 
   $vector1 = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo "The vector1 elements: \n"; 
   print_r($vector1);
   $vector2 = new \Ds\Vector([6, 7, 8, 9, 10]); 
   echo "The vector2 elements: \n";
   print_r($vector2);
   echo("The resultant vector: \n"); 
   print_r($vector1->merge($vector2)); 
?>

输出

上述程序产生以下输出:

The vector1 elements:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The vector2 elements:
Ds\Vector Object
(
    [0] => 6
    [1] => 7
    [2] => 8
    [3] => 9
    [4] => 10
)
The resultant vector:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

示例 2

以下是 PHP Ds\Sequence::merge() 函数的另一个示例。我们使用此函数来检索将给定向量序列中的所有值添加到序列的结果:

<?php 
   $vector1 = new \Ds\Vector(["Tutorials", "Point", "Tutorix"]); 
   echo "The vector1 elements: \n"; 
   print_r($vector1); 
   $vector2 = new \Ds\Vector(["Hyderabad", "Madhapur", "India"]);
   echo "The vector2 elements: \n";
   print_r($vector2);   
   echo("The vector after called the merge() function: \n"); 
   print_r($vector1->merge($vector2));
?>

输出

执行上述程序后,将显示以下输出:

The vector1 elements:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
The vector2 elements:
Ds\Vector Object
(
    [0] => Hyderabad
    [1] => Madhapur
    [2] => India
)
The vector after called the merge() function:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
    [3] => Hyderabad
    [4] => Madhapur
    [5] => India
)
php_function_reference.htm
广告