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
廣告