
- PHP 教程
- PHP - 首頁
- PHP - 路線圖
- PHP - 簡介
- PHP - 安裝
- PHP - 歷史
- PHP - 特性
- PHP - 語法
- PHP - Hello World
- PHP - 註釋
- PHP - 變數
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 變數
- PHP - 常量
- PHP - 魔術常量
- PHP - 資料型別
- PHP - 型別轉換
- PHP - 型別混淆
- PHP - 字串
- PHP - 布林值
- PHP - 整數
- PHP - 檔案與 I/O
- PHP - 數學函式
- PHP - Heredoc & Nowdoc
- PHP - 複合型別
- PHP - 檔案包含
- PHP - 日期與時間
- PHP - 標量型別宣告
- PHP - 返回型別宣告
- PHP 運算子
- PHP - 運算子
- PHP - 算術運算子
- PHP - 比較運算子
- PHP - 邏輯運算子
- PHP - 賦值運算子
- PHP - 字串運算子
- PHP - 陣列運算子
- PHP - 條件運算子
- PHP - 展開運算子
- PHP - 空值合併運算子
- PHP - 比較運算子(spaceship)
- PHP 控制語句
- PHP - 決策
- PHP - If…Else 語句
- PHP - Switch 語句
- PHP - 迴圈型別
- PHP - For 迴圈
- PHP - Foreach 迴圈
- PHP - While 迴圈
- PHP - Do…While 迴圈
- PHP - Break 語句
- PHP - Continue 語句
- PHP 函式
- PHP - 函式
- PHP - 函式引數
- PHP - 按值傳遞
- PHP - 按引用傳遞
- PHP - 預設引數
- PHP - 具名引數
- PHP - 可變引數
- PHP - 返回值
- PHP - 傳遞函式
- PHP - 遞迴函式
- PHP - 型別提示
- PHP - 變數作用域
- PHP - 嚴格型別
- PHP - 匿名函式
- PHP - 箭頭函式
- PHP - 可變函式
- PHP - 區域性變數
- PHP - 全域性變數
- PHP 超全域性變數
- PHP - 超全域性變數
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 檔案處理
- PHP - 檔案處理
- PHP - 開啟檔案
- PHP - 讀取檔案
- PHP - 寫入檔案
- PHP - 檔案是否存在
- PHP - 下載檔案
- PHP - 複製檔案
- PHP - 追加檔案
- PHP - 刪除檔案
- PHP - 處理 CSV 檔案
- PHP - 檔案許可權
- PHP - 建立目錄
- PHP - 列出檔案
- 面向物件的 PHP
- PHP - 面向物件程式設計
- PHP - 類和物件
- PHP - 建構函式和解構函式
- PHP - 訪問修飾符
- PHP - 繼承
- PHP - 類常量
- PHP - 抽象類
- PHP - 介面
- PHP - Traits
- PHP - 靜態方法
- PHP - 靜態屬性
- PHP - 名稱空間
- PHP - 物件迭代
- PHP - 封裝
- PHP - final 關鍵字
- PHP - 過載
- PHP - 克隆物件
- PHP - 匿名類
- PHP Web 開發
- PHP - Web 概念
- PHP - 表單處理
- PHP - 表單驗證
- PHP - 表單郵件/URL
- PHP - 完整表單
- PHP - 檔案包含
- PHP - GET & POST
- PHP - 檔案上傳
- PHP - Cookie
- PHP - Session
- PHP - Session 選項
- PHP - 傳送郵件
- PHP - 輸入過濾
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash 訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 程式碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - 除錯
- PHP - 針對 C 開發人員
- PHP - 針對 PERL 開發人員
- PHP - 框架
- PHP - Core PHP vs 框架
- PHP - 設計模式
- PHP - 過濾器
- PHP - JSON
- PHP - 異常
- PHP - 特殊型別
- PHP - 雜湊
- PHP - 加密
- PHP - is_null() 函式
- PHP - 系統呼叫
- PHP - HTTP 認證
- PHP - 交換變數
- PHP - Closure::call()
- PHP - 過濾後的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 預期
- PHP - Use 語句
- PHP - 整數除法
- PHP - 已棄用的特性
- PHP - 已移除的擴充套件和 SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 程序
- PHP - PDO 擴充套件
- PHP - 內建函式
- PHP 有用資源
- PHP - 速查表
- PHP - 問答
- PHP - 快速指南
- PHP - 線上編譯器
- PHP - 有用資源
- PHP - 討論
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
廣告