首頁  >  文章  >  後端開發  >  PHP 數組函數

PHP 數組函數

WBOY
WBOY原創
2024-08-29 12:44:50364瀏覽

PHP 陣列函數(超文本預處理器的縮寫)是一種廣泛使用的通用腳本語言;它與 HTML 和 Web 開發的兼容性使其成為易於理解的關鍵技術。 PHP 中的陣列是指可以在單一

中保存或儲存多個值的變數類型

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

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

可以輕鬆嵌入HTML中,讓我們用簡述來見證 –

代碼:

<!DOCTYPE html>
<html>
<head>
<title>……………</title>
</head>
<body>
<?php
echo  "Hello, I am a PHP !";
?>
</body>
</html>

輸出:

PHP 數組函數

上面的腳本檔案非常清楚地證明了 PHP 腳本與 HTML 的兼容性有多好。 PHP 程式碼包含特殊的開始和結束括號。

如何在 PHP 中建立陣列?

陣列()

下面我們列出了陣列的工作原理 –

$color = array("red", "green", "blue");

輸出

$color[0] = “紅色”
$color[1] = “綠色”
$color[2] = “藍色”

這裡的目的是將顏色名稱儲存在一個顏色變數中。所以我們在數組函數中有一個顏色變量,在這個函數中,我們以字串格式一一命名了所有顏色。

PHP 陣列函數

有 3 種不同類型的陣列:

  • 數值數組
  • 關聯數組
  • 多維數組

這三個解釋如下:

1.數值數組

數字數組是那些具有數字索引的數組。讓我們看看數值數組的語法 - 有兩種類型的語法。

第一種方式:

$array_name[0] = value;

第二種方式:

$array_name[] = value;
注意:這裡方括號[0]中的零代表索引號。

值表示使用者想要在陣列中儲存的內容。

第一種和第二種語法有一些區別,一種是 [] 中為零,另一種是 [] 為空白。

預設情況下,所有數組都以索引 0 開頭,這意味著對於第一個數組,如果我們在 [] 中輸入 0 或將其留空 [] 都表示相同的意思。再看一個例子以更好地理解差異

$array_name[] = value; {either you put 0 or leave it blank – both means same}
$array_name [1] = value;

下面列出了具有不同值和不同索引的陣列 –

$name[0] = "Alex";
$name[1] = "Peter";
$name[2] = "Lucy"

2.關聯數組

關聯數組是以字串作為索引的陣列。儲存的值是與鍵值關聯執行的,而不是線性索引。

讓我們看看關聯數組的語法。

$array_name["key"] = value;
注意:稱之為鍵或稱為索引(兩者俱有相同的含義)。

當您必須在值和鍵(或索引)之間建立關係時,請使用關聯數組。

3.多維數組

多維數組是包含一個或多個數組以及其中的值的數組。這些數組可以透過多個索引來存取。

在單一定義中,我們可以將多維稱為陣列的陣列。多維數組可以是 1D(I 維)、2D(2 維)….n 維。

Alex England 23
Peter Germany 26
Lucy Holland 27

因此,如果我們以 2D 形式存儲,分配將是下面列出的內容 –

Alex [0][0] England[0][1] 23[0][2]
Peter[1][0] Germany[1][1] 26[1][2]
Lucy[2][0] Holland[2][1] 27[2][2]

The same goes for ‘n’ number of dimensions and allocations.

Examples on Types of the Array Function

Let us see the types of the array with the help of an example:

1. Numeric Array

Code:

<html>
<body>
<?php
$numbers[] = "eleven";
$numbers[] = "twelve";
$numbers[] = "thirteen";
$numbers[] = "fourteen";
$numbers[] = "fifteen";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>

Output:

PHP 數組函數

2. Associative Array

Code:

<html>
<body>
<?php
$salaries['Alex'] = "high";
$salaries['Peter'] = "medium";
$salaries['Lucy'] = "low";
echo "Salary of Alex is ". $salaries['Alex'] . "<br />";
echo "Salary of Peter is ". $salaries['Peter']. "<br />";
echo "Salary of Lucy is ". $salaries['Lucy']. "<br />";
?>
</body>
</html>

Output:

PHP 數組函數

3. Multidimensional array

Code:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Multidimensional Array</title>
</head>
<body>
<?php
// Define nested array
$contacts = array(
array(
"name" => "Petergomes",
"email" => "[email protected]",
),
array(
"name" => "Clark anthony",
"email" => "[email protected]",
),
array(
"name" => "lucy disilva",
"email" => "[email protected]",
)
);
// Access nested value
echo "Peter gomes's Email-id is: " . $contacts[0]["email"];
?>
</body>
</html>

Output:

PHP 數組函數

Advantages

Following are some of the advantages described.

  • When your intention is to represent multiple data that belong to the same type with using only single indexing naming.
  • It has wide applicability as it can be used to implement other data structures like stacks, trees, queues, graphs, and linked lists.
  • 2D/3D arrays are used to represent matrices effectively
  • It has less coding with the elimination of complexity
  • Sorting can be done easily

Conclusion

PHP arrays hold crucial importance in PHP programming, it acts as the ultimate variable of PHP. It behaves as a storage container for collecting elements. Arrays also can store other variables within like strings, integers, and even other arrays. If you have to deal with an unknown amount of variables you must prefer to work using arrays. Loops can be used to output values in arrays, also by simply calling specific elements with the index or key values.

以上是PHP 數組函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn