首頁  >  文章  >  後端開發  >  PHP 中的索引數組

PHP 中的索引數組

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

在下面的文章中,我們將討論 PHP 中的索引陣列。陣列是一種資料結構,或更像是上面討論的儲存位置,它以單一名稱儲存一個或多個相同類型的資料。理解這一點的另一種方法是,我們擁有結構數組中每個值的鍵,因此當我們的單一變數保存項目或值的清單時,我們可以使用這些鍵來識別它們中的每一個。這種資料結構的最佳化可以用作數組、字典或值的集合、堆疊佇列等。並且由於數組內的值也可以是數組本身,因此我們有可能創建一棵樹或多維數組。

數組類型

現在,根據陣列中使用的鍵的大小和類型(可以是字串或整數),我們主要建立三種類型的陣列。可以建立任何類型的值。因此,類型可以建立為數字或索引數組、關聯數組和多維數組。

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

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

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

PHP 中的索引數組

  1. 索引或數字數組:我們的鍵本質上是數字的類型,即數字索引。這裡的值是線性存取並以線性方式儲存的。
  2. 關聯陣列:這種類型的陣列以字串作為存取值的鍵,且其鍵和值之間存在強烈關聯。它類似於索引數組。
  3. 多維數組:多維數組可以包含一個數組,該數組內部可能包含另一個數組,依此類推。在多維數組中,使用不同的索引來存取值。

索引數組

正如上面簡要討論的,索引數組是一種透過數字索引來存取其值的數組類型。但是,它們可以儲存數字、字元、字串等。預設情況下,如果不指定,陣列索引將以數字表示,從索引 0 開始,以索引 -1 結束.

建立索引數組主要有兩種方法。

  1. 第一個是簡單地為每個值「手動」分配一個索引並建立陣列。
  2. 第二個,我們可以使用不帶任何索引的array()函數,索引將預設分配,並且第一個元素或值從0開始。我個人比較喜歡這個方法。

讓我們一一看看創建數組的兩種方法。

手動索引分配:在下面給出的範例中,我們手動將索引一一分配給我們的值。

<?php
$employee[0] = "Ram";
$employee[1] = "Male";
$employee[2] = "28";
echo "My name is ".$employee[0].", I am ".$employee[2] . "  years old and my gender is ".$employee[1].".";
?>

上面的範例程式碼將產生以下輸出:

PHP 中的索引數組

您也可以在下面給出的程式截圖及其在即時環境中的輸出中看到相同的程式碼。

函數 array(): 下面寫的程式碼使用 array() 函數建立一個名為 $autos 的索引陣列。該函數正在將三個元素指派給我們的陣列名稱。

然後,我們形成一個包含數組值的簡單文字行,並使用 echo 語句列印它們。

代碼:

<?php
$employee = array("Ram", "Male", "28");
echo "My name is ".$employee[0].", I am ".$employee[2] . "  years old and my gender is ".$employee[1].".";
?>

輸出:

PHP 中的索引數組

注意:我們先存取$employee[2]索引,然後根據需要呼叫$employee[1]。

但是如果我的陣列中有幾十個值並且需要列印它們怎麼辦?

使用帶有 echo 語句的分隔符號輸入數組中的所有值來列印所有值會很麻煩。為此,一個簡單的方法是我們可以遍歷完整的陣列並能夠列印值。在索引數組中遍歷索引數組簡單又容易;在這裡,我們使用循環。

在 PHP 中遍歷索引數組

遍歷數組意味著將數組的值一一讀取,並在需要時進行列印。可以輕鬆遍歷索引數組;我們簡單地使用「循環遍歷值」方法。我們將使用 for 迴圈或 foreach 迴圈來遍歷索引數組,然後列印所有所需的值。

代碼:

<?php
$employee = array("Ram", "Male", "28");
$length = count($employee);
for($x = 0; $x < $length; $x++)
{
echo $employee[$x];
echo "<br/>";
}
?>

輸出:

PHP 中的索引數組

The above program prints the contents of our array.

Note: The values Ram, Male and 28 are printed in new lines because of the break statement (
) we used in our code.

Code:

<?php
$employee = array("Ram", "Male", "28");
foreach($employee as $e)
{
echo "$e <br/>";
}
?>

Output:

PHP 中的索引數組

You can see the above simple code and its output in the live environment in the following screenshot.

Another commonly used method in arrays is to fetch the length of the array. The count() function is used for this purpose. Following is a simple PHP code creating an array and then returning its length. We used the count() function, which is returning the length, i.e. the number of elements our array contains, as shown in the output.

Code:

<?php
$employee = array("Ram", "Male", "28");
echo count($employee);
?>

Output:

PHP 中的索引數組

The output is 3 (see in the above screenshot), which is equal to the total number of elements or values in our array $employee.

Conclusion

In simple words, arrays tend to show special characteristics with a capacity to store several values in one variable. They are quite stretchable; that is, if one needs to add more values afterward, it can be done with ease. An indexed array is a smarter way to bind all related information together, for example, an employee’s details. It also helps in writing clean code.

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

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