PHP 程式語言的 array_push() 函數實際上是一個內建函數,它有助於根據我們的要求將新元素推送到特定的陣列中。我們可以根據需要將一個或多個元素推送到特定數組中,這些數組元素將插入最後一個節/索引值位置。由於使用 array_push() 函數,特定數組的長度將根據推入特定數組的元素數量而增加/增加。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
PHP array_push() 的語法和參數為:
array_push($array1, $value1, $value2, $value3, …..)
array_push()函數的參數說明:
PHP 程式語言的 array_push() 函數內部將有多個可用參數。 array_push() 函數的參數數量基本上取決於實際推入特定數組的元素數量。具體可以將這些參數分為兩類。它們是 1. $array1, 2. 值列表
PHP 程式語言的 array_push() 函數基本上只是將一些元素推入特定陣列。 array_push() 函數也可以將多個元素推送到實際在 array_push() 函數內部指定的原始陣列中。使其工作後,數組的長度將增加,並且基於推入數組的元素數量。如果陣列具有鍵和值對,則該方法將嘗試將數字鍵新增至推送的值。 PHP 的 array_push() 函數只在 PHP 4、PHP 5 和 PHP 7 版本上執行。
這是藉助原始數組參數和值列表參數說明 array_push() 函數的範例。首先在 PHP 標籤
代碼:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array1 = array("ram", "krishna", "aakash"); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array1); echo "<hr>"; // elements to push $value1 = "pavan"; $value2 = "kumar"; $value3 = "sake"; $value4 = "anil"; $value5 = "maruthi"; $value6 = "raj"; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array1, $value1, $value2, $value3, $value4, $value5, $value6); print_r($array1); echo "<hr>"; ?>
輸出:
此範例與範例 1 類似,但不同之處在於 array() 函數內部宣告/提到了 Key 和 value 參數(提到了 Key_value 對)。除此之外,一切都與範例 1 非常相似。您可以檢查下面輸出部分中提到的程式的輸出,以便更好、更輕鬆地理解 array_push() 函數。
代碼:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array2 = array(1=>"rahim", 2=>"krishnaveni", 3=>"lion"); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array2); echo "<hr>"; // elements to push $valuea1 = "pavan"; $valuea2 = "sake"; $valuea3 = "kumar"; $valuea4 = "king"; $valuea5 = "queen"; $valuea6 = "birbal"; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array2, $valuea1, $valuea2, $valuea3, $valuea4, $valuea5, $valuea6); print_r($array2); echo "<hr>"; ?>
輸出:
This example is a simple illustration of the array_push() function but here only some integer values are used as the array elements. Then four variables are created with some integer values to it. Then all those four variable values are pushed into the original array with the help of array_push() function. Other than this everything is similar to example 1 and 2. You can check the output below to understand the concept of array_push() better and so easily.
Code:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array2 = array(2, 42, 8); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array2); echo "<hr>"; // elements to push $valuea1 = 12; $valuea2 = 13; $valuea3 = 14; $valuea4 = 15; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array2, $valuea1, $valuea2, $valuea3, $valuea4); print_r($array2); echo "<hr>"; ?>
Output:
以上是PHP array_push()的詳細內容。更多資訊請關注PHP中文網其他相關文章!