PHP - Ds Vector::insert() 函数



PHP 的 Ds\Vector::insert() 函数用于在向量中指定索引处插入值。索引是向量中元素的位置,其中索引 0 表示第一个元素,1 表示第二个元素,依此类推。

使用此函数,您可以一次插入多个值,如果指定的索引值无效,则该函数会抛出“OutOfRangeException”异常。

语法

以下是 PHP Ds\Vector::insert() 函数的语法:

public Ds\Vector::insert(int $index, mixed ...$values): void

参数

以下是此函数的参数:

  • index - 要插入值的索引。
  • values - 需要插入的一个或多个值。

返回值

此函数不返回值。

示例 1

以下是 PHP Ds\Vector::insert() 函数的基本示例:

<?php 
   $vector = new \Ds\Vector([1, 2, 4, 5]);
   echo "The vector elements are: \n";   
   print_r($vector);
   $index = 2;
   $value = 3;
   echo "The index and given value is: ".$index.", ".$value;  
   echo "\nThe vector after inserting new element: \n";
   #using insert() function
   $vector->insert($index, $value);
   print_r($vector);   
?>

输出

以上程序输出如下:

The vector elements are:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
)
The index and given value is: 2, 3
The vector after inserting new element:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

示例 2

以下是 PHP Ds\Vector::insert() 函数的另一个示例。我们使用此函数在该向量 (["Tutorials", "Point", "Turorix"]) 中的给定索引 0 处插入指定的元素“India”:

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "Turorix"]);
   echo "The original vector elements are: \n";
   print_r($vector);
   $index = 0;
   $value = "India";
   echo "The index and given value is: ".$index.", ".$value;
   echo "\nThe updated vector is: \n";
   #using insert() function
   $vector->insert($index, $value); 
   print_r($vector);
?>

输出

执行上述程序后,将生成以下输出:

The original vector elements are:
Ds\Vector Object
(
    [0] => Tutorials	
    [1] => Point
    [2] => Turorix
)
The index and given value is: 0, India
The updated vector is:
Ds\Vector Object
(
    [0] => India
    [1] => India
    [2] => Tutorials
    [3] => Point
    [4] => Turorix
)

示例 3

一次在向量中指定索引处插入多个值

在下面的示例中,我们使用insert() 函数一次在向量中指定的索引 0 处插入指定的值 'd'、'e' 和 'f':

<?php 
   $vector = new \Ds\Vector(['a', 'b', 'c']);
   echo "The original vector elements are: \n";
   print_r($vector);
   $index = 0;
   $v1 = 'd';
   $v2 = 'e';
   $v3 = 'f';
   echo "The index is: ".$index;
   echo "\nThe given values are: ".$v1.", ".$v2.", ".$v3;
   echo "\nThe updated vector is: \n";
   #using insert() function
   $vector->insert($index, $v1, $v2, $v3); 
   print_r($vector);
?>

输出

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

The original vector elements are:
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
)
The index is: 0
The given values are: d, e, f
The updated vector is:
Ds\Vector Object
(
    [0] => d
    [1] => e
    [2] => f
    [3] => a
    [4] => b
    [5] => c
)

示例 4

如果指定的索引无效,此函数将抛出“OutOfRangeException”异常:

<?php 
   $vector = new \Ds\Vector([10, 20, 30]);
   echo "The original vector elements are: \n";
   print_r($vector);
   $index = 10;
   $value = 40;
   echo "The index is: ".$index;
   echo "\nThe given value is: ".$value;
   echo "\nThe updated vector is: \n";
   #using insert() function
   $vector->insert($index, $value); 
   print_r($vector);
?>

输出

以上程序抛出以下异常:

The original vector elements are:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
)
The index is: 10
The given value is: 40
The updated vector is:
PHP Fatal error:  Uncaught OutOfRangeException: 
Index out of range: 10, expected 0 <= x <= 3 in C:\Apache24\htdocs\index.php:11
Stack trace:
#0 C:\Apache24\htdocs\index.php(11): Ds\Vector->insert(10, 40)
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 11
php_function_reference.htm
广告