首頁  >  文章  >  後端開發  >  什麼是 PHP 數組?

什麼是 PHP 數組?

王林
王林原創
2024-08-29 12:42:37180瀏覽

PHP 中的陣列是一種資料結構,它使我們能夠省去創建不同變數的工作,以便在單一變數下儲存具有相似資料類型的多個元素。數組有助於建立相似元素的列表,可透過索引或鍵存取。此處使用陣列將每個元素儲存在一個變數中,並且還可以透過索引或鍵輕鬆存取。使用 PHP 中的函數,產生一個陣列。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

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

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

PHP 中的陣列是什麼?

陣列不過是允許在一個名稱下儲存多個值的資料結構。簡單來說,它就是一個能夠包含多個值的變數。分配給數組的值本質上是同質的,這意味著所有值的資料類型應該相同。如果陣列應該只包含整數值,那麼就不可能在其中儲存字元或字串值。

陣列也可以被視為項目的集合,它遵循特定的順序來儲存項目或值。建立數組後,我們可以為其分配多個值。在 C、C++ 等語言中,開發人員最初應該指定數組的大小,但在 PHP 中,我們可以隨意添加所需數量的項目,數組的大小也會相應增加。

理解 PHP 陣列

現在我們將嘗試使用語法和範例來理解 PHP 中的陣列。 PHP 陣列是透過使用陣列函數來實現的,該函數寫為「array()」。它有一個非常簡單的語法,使得數組的使用非常容易。以下是在 PHP 中宣告和初始化數組的語法。

$array_name = array("value1", "value2", "value3",..)

這裡,

  • Array_name 是將由值組成的陣列的名稱。
  • Array() 是 PHP 中幫助實作陣列機制的函數。
  • 雙引號內的值是分配給陣列的值。

使用上面的語法,我們可以將值分配給陣列。好吧,你能猜出我們如何使用數組中儲存的值嗎?讓我們來了解如何使用數組值的語法。

$array_name[index]

這裡,

  • Array_name 是我們在上述語法中定義的陣列的名稱。
  • 索引是值的位置。例如,「value1」的索引值為 0。因此,要使用它們 value1,我們必須將其寫成 $array_name[0].

PHP 陣列如何讓工作變得如此簡單?

陣列使得程式的使用這一事實是不容置疑的。在本節中,我們將了解陣列實際上如何使工作變得輕鬆,並且我們將考慮一些範例來準確了解 PHP 陣列的功能。

範例:下圖將接受多個學生的姓名並列印出來。

<?php
$student_name=array("Amit", "Raj", "Dhiraj", "Shyam");
echo $student_name[0];
echo "<br>";
echo $student_name[1];
echo "<br>";
echo $student_name[2];
echo "<br>";
echo $student_name[3];
?>

輸出:上面的程式會列印所有學生的姓名,下面是輸出。

阿米特
拉吉
迪拉吉
夏姆

下圖會讓你清楚陣列的概念。它顯示了為新增到陣列的項目分配值的順序。

什麼是 PHP 數組?

PHP 陣列的型別

在 PHP 中,我們有兩種類型的陣列:一維數組和多維數組。

1. One-dimensional array

  • The one-dimensional array may be defined as the type of array that consists of multiple values assigned to the single array.
  • The Values assigned in the one-dimensional array are homogenous in nature that means it allows only the values with the same data type to be added to the array.
  • Syntax will be like.
    $array_name = array("value1", "value2", "value3")
  • The way to use the values from the single dimensional array is the same that we have discussed above. $array_name[2] could be used to bring “Value3” in use.

2. Multi-dimensional array

  • A multi-dimensional array may be defined as the kind of array that allows us to assign values in multiple arrays at the same time.
  • The values assigned to the multi-dimensional array could be heterogeneous that means values of different data types could be stored in the arrays and it has to be taken care that the sequence of items of specific data types should be same in all the arrays.
  • Here we will look on two-dimensional array syntax. Well, just for your information, the complexity of array increases as the dimension of the array increases.
$array_name = array
(
array("String1", "Int1", "Int11"),
array("Stirng2", "Int2", "int22"),
array("String3", "Int3", "int33"),
array("String4", "Int4", "int44"),
)
  • To access the value stored in two-dimensional array we will use below syntax.
$array_name[0][0] will represent "String1"
$array_name[0][1] will represent "int1"
$array_name[0][2] will represent "int11"
$array_name[2][0] will represent "String3"
$array_name[2][1] will represent "int3"
$array_name[2][2] will represent "int33"

What can you do with PHP Array?

Array in PHP is very helpful while dealing with complex programs. It plays a sheer crucial role in order to make the program less bulky. For illustration, in order to store 10 values we will need 10 different variables but by the use of an array in PHP, we can store 10 different values of the same data type in one array. By the virtue of array, the program could be made very efficient as it helps to save the memory that is usually consumed by the allocation of variables

For simple programs, a one-dimensional array could be very helpful and if the program is somewhat complex than a developer can leverage the multi-dimensional array to get the expected outcome. In PHP, there is no need to assign the data type of the array. It automatically detects the data type by analyzing the values or items assigned to it. In addition to all the features of an array, it’s characteristic of increasing the size automatically makes it favorite among the developers.

Working with PHP array

In the above sections, we learned how an array is implemented in PHP, the way it stores the value and how we can use the values assigned to the array. Now in this section, we see how array could be used together with some of the inbuilt functions to get the desired output. As we know the best way to learn anything is facilitated by examples, the same way we will have a look in an example to dive deeper on working with an array.

Example: In this example, we will use count function with the array to find the total number of students in a school.

<?php
$studnet_name[0]="Amit";
$studnet_name[1]="Raj";
$studnet_name[2]="Dhiraj";
$studnet_name[3]="Shyam";
$arraylenght = count($studnet_name);
echo "The total number of students is" . $arraylenght;
?>

Output: The count function will count the number of items assigned to the array and that will help us find the total number of students.

The total number of students is 4

The way we added items to the array is different that we used initially. It has been used here this way to give you an idea of the alternate way of assigning values to the array. Name of the four students are assigned to each index of the array and could be used by choosing the index accordingly. let’s see another example where we will be using the sort and count function collectively to sort the values in the array.

Example: In this example, we will assign several roll numbers to the array in some random order and then will make the use of sort function to arrange the order of roll numbers.

<?php
$roll_number= array("22", "13", "2", "9");
sort($roll_number);
$roll_length = count($roll_number);
echo "Roll numbers in ascending order :"
for($x = 0; $x < $roll_length; $x++)
{
echo $roll_number[$x] . " ";
}?>

Output: The roll numbers that are assigned to the array in the random series will get sorted in ascending order.

Roll numbers in ascending order: 2 9 13 22

Advantages

We have been watching out the pros of using the PHP array and now we will be arranging those in a single section. Below are some of the advantages of PHP array that makes it very useful.

1. Multiple values under one name

An Array allows us to add multiple items of a similar type under one name which makes the values easy to access and convenient as well. Though the values are supposed to be of the same data type, we will not need to define the type of the array.

2. Endorse data structure implementation

The data structures such as linked list, trees, queues are implemented in any high-level programming language by the use of an array. Due to this advantage of an array, the languages could provide a platform to work on complex applications as well. It actually facilitates the way data has to be stored in the memory.

3.矩陣表示

多維數組允許開發人員透過以與數組中保存的方式相同的方式排列值來執行矩陣運算。使用矩陣時必須注意的唯一注意事項是,輸出應該是一維數組或二維數組,我們相應地選擇了輸出數組。

4.容易記住

我們要記住的是索引號,以便找到特定的值。無需記住多個變數名稱,因為所有值都可以使用相同的名稱來呼叫或使用,這實際上是數組名稱。

5.記憶體利用率較低

變數數量的增加也會增加程式的記憶體使用率,因為需要將記憶體分配給每個變量,但如果是數組,則必須將記憶體分配給可以儲存多個值和相同不必要的值的單個數組記憶體消耗。

所需技能

要使用 PHP 數組,您應該了解一些有關程式設計的基本知識,例如如何將值分配給數組,應該如何呼叫它們等等。為了使用數組實現資料結構,必須了解資料結構部分以及數組的工作概念。

我們還需要了解變量,因為數組只不過是變數的擴展版本。變數儲存單一值的方式,陣列使用索引儲存單一值的方式相同,將多個索引的平均值相加的單一值組合在一起形成一個陣列。任何對程式設計基礎有深入了解的人都可以非常輕鬆地使用陣列。

為什麼我們需要 PHP 陣列?

為了得到這個問題的答案,我們必須提醒我們到目前為止在本教程中所理解的所有內容。很明顯,只有當任何東西為我們提供某種優勢時,我們才更喜歡使用它,並且由於數組在各個方面對我們非常有利,因此我們總是更喜歡在需要時使用數組。在編寫程式碼時,每當我們需要為某個常見實體儲存多個值時,我們首先想到的就是一個陣列。

如前所述,在程式中引入資料結構的唯一方法是透過數組來實現。樹、堆疊、佇列、鍊錶是定義資料儲存方式的一些資料結構。要實現這些資料結構,數組是唯一也是最好的選擇。除了其功能之外,它還非常易於使用。此外,我們需要它,因為它為我們提供了減少記憶體使用的機會。使用陣列的程式開發人員總是非常有效率並且消耗更少的記憶體。

誰是學習 PHP 陣列的合適受眾?

任何願意學習PHP的人都是學習PHP陣列的完美受眾。學習數組所需要的只是了解程式設計基礎知識。因此,更具體地說,任何了解編碼基礎知識的人都能夠順利理解數組。

學習陣列將如何幫助您的職涯發展?

由於現在需要處理大量數據,以系統的方式存儲數據變得非常重要,以便可以在需要時獲取數據,並且這就是數組的準確用武之地。支持您作為開發人員的職業生涯,它還將為您提供將您的職業發展到資料結構技術的機會

結論

在任何程式語言中,陣列都起著非常重要的作用,因此在開發複雜程式時大部分時間都需要使用陣列。它提供了一種有效的記憶體利用機制,使其成為處理大量資料時最需要的東西。

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

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