PHP array_push()

王林
發布: 2024-08-29 12:45:37
原創
841 人瀏覽過

PHP 程式語言的 array_push() 函數實際上是一個內建函數,它有助於根據我們的要求將新元素推送到特定的陣列中。我們可以根據需要將一個或多個元素推送到特定數組中,這些數組元素將插入最後一個節/索引值位置。由於使用 array_push() 函數,特定數組的長度將根據推入特定數組的元素數量而增加/增加。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

語法與參數

PHP array_push() 的語法和參數為:

array_push($array1, $value1, $value2, $value3, …..)
登入後複製

array_push()函數的參數說明:

PHP 程式語言的 array_push() 函數內部將有多個可用參數。 array_push() 函數的參數數量基本上取決於實際推入特定數組的元素數量。具體可以將這些參數分為兩類。它們是 1. $array1, 2. 值列表

  • array_push() 函數的$array1 參數: array_push() 函數的$array1 參數實際上是實際指定或操作的原始陣列。它是主數組,包含之前定義的所有數組元素。
  • 值列表(多個值參數):值列表是 PHP 程式語言的 array_push() 函數的多個參數。這個參數是一堆實際上用逗號分隔的元素列表,這些分隔的元素將被推入某個特定的陣列中。讓這些陣列為 $value1、$value2、$Value3、$Value4 等等。
  • array_push() 函數的回傳值: PHP 程式語言的 array_push() 函數只會透過引用的參數值新增/推送一些元素來傳回修改後的陣列到 array_push() 函數內部。新增的這些元素將根據我們的要求放置在一個/多個陣列的最後一個索引值處。

array_push() 函數在 PHP 中如何運作?

PHP 程式語言的 array_push() 函數基本上只是將一些元素推入特定陣列。 array_push() 函數也可以將多個元素推送到實際在 array_push() 函數內部指定的原始陣列中。使其工作後,數組的長度將增加,並且基於推入數組的元素數量。如果陣列具有鍵和值對,則該方法將嘗試將數字鍵新增至推送的值。 PHP 的 array_push() 函數只在 PHP 4、PHP 5 和 PHP 7 版本上執行。

範例#1

這是藉助原始數組參數和值列表參數說明 array_push() 函數的範例。首先在 PHP 標籤


內部標籤用於水平線。之後,在 array() 函數的幫助下使用一些字串數組索引值/元素創建一個數組變量,但這裡的鍵沒有定義。然後原始數​​組元素將在“print_r()”函數的幫助下列印。然後創建一些值變數並在其中儲存一些字串值。這裡創建了六個帶有值的字串變數。然後 array_push() 函數與原始變數和傳遞給它的所有六個字串變數一起使用。這會將所有提到的元素推送到特定數組中。然後 print_r($array1) 函數將列印包含所有額外元素的陣列。

代碼:

<?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>";
?>
登入後複製

輸出:

PHP array_push()

範例#2

此範例與範例 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>";
?>
登入後複製

輸出:

PHP array_push()

Example #3

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 array_push()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!