首頁 >後端開發 >php教程 >PHP 中的二維數組

PHP 中的二維數組

WBOY
WBOY原創
2024-08-29 12:43:14818瀏覽

數組是任何資料類型的元素的集合。 php中有很多資料類型,如字串、整數、布林值、陣列、物件、資源…等。二維數組是這些資料類型的混合,主要是數組。 PHP 中有以下三種不同類型的二維數組:

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

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

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

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

PHP 中的二維數組類型

這三個陣列解釋如下:

1.數字數組

有數字索引的陣列。

文法:

array(value1, value2, value3, …);

範例

$input = array(10,20,30,40,50);

2.關聯數組

具有字串或數字索引的陣列。此數組的元素以鍵值對的形式儲存。

文法:

array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3',…);

範例

$input =  array(0 =>Emma, 1=>Alice, 2=>'John');

3.多維數組

陣列的陣列是多維數組或二維數組或巢狀數組。此格式始終是數組或數組。因此稱為嵌套數組。

文法:

array (
array (elements...),
array (elements...),
...
)

範例:

$input = array(
array( "red", "green", "blue" ),
array( "yellow", "black", "white" )
);

在上面的範例中,輸入數組是二維數組的範例。在這裡,主數組包含 2 個元素,其中每個元素本身又是包含 3 個元素的陣列。

如何定義二維數組?

我們了解到,在二維數組中,值元素是一個數組,並且還可能有子數組。數組中提到的維度採用行和列的形式。記住陣列的表格格式,可以更輕鬆地學習如何定義這些陣列。意思是,如果是二維數組,將使用兩個索引,類似地,如果是三維數組,將使用三個索引,依此類推。

如何建立二維數組?

既然我們知道如何定義二維數組,我們現在就可以創建它了。這裡索引沒有定義,預設是一個始終以 0 開頭的數字。

$input=array(
array( "red", "green", "blue" ),
array( "yellow", "black", "white" )
);

陣列也可以以關聯數組的形式定義。

(in key =>value form)

索引或鍵是一個字串,如顏色、水果和汽車。值元素採用陣列的形式,每個元素包含 3 個元素。

$input = array(
'colors'=>array ("Red", "Green", "Blue"),
'fruits'=>array ("Apple", "Orange", "Grapes"),
'cars'=>array ("BMW", "Skoda", "Mercedes")
);

如何存取二維數組的元素?

要存取這些陣列值,您可以使用方括號。當您深入二維數組的更多層級時,方括號組的使用將隨著每個層級的增加而增加。

範例#1

代碼:

$input = array (
'colors' =>array ("Red", "Green", "Blue"),
'fruits' =>array ("Apple", "Orange", "Grapes"),
'cars' =>array ("Skoda", "BMW", "Mercedes")
);

請記住,第一組方括號包含鍵,在本例中是顏色、水果、汽車。接下來是一組方括號,用於向下遍歷下一層,可以使用 0,1,2 等數字進行存取。

所以如果我們想訪問上面數組中的元素“Grapes”,

echo $input['fruits'][2];

類似下面的例子

如果我們想存取數組中的元素“Mercedes”,那麼

echo $input['cars'][2];

如果我們想存取數組中的元素“Red”,那麼

echo $input['colors'][0];

因為數組中索引總是從 0 開始。

範例#2

代碼:

$input = array (
array ("Red", "Green", "Blue"),
array ("Yellow", "Orange", "Purple"),
);

如果我們想存取上面數組中的元素“Orange”,我們將使用以下行

echo $input[0][1];

將給予「綠色」

echo $input[1][2];

將給出「紫色」

echo $input[0][0];

將給出「紅色」

如何在 PHP 中插入二維陣列的元素?

既然我們知道如何定義、建立和存取陣列元素,我們現在將學習如何在陣列中插入元素。 PHP 中定義了一些陣列函數來處理多維數組,例如用於插入的 array_push() 函數、用於刪除的 array_shift() 函數等等。

$input = array (
'colors'=>array ("Red", "Green", "Blue"),
'fruits'=>array ("Apple", "Orange", "Grapes"),
'cars'=>array ("Skoda", "BMW", "Mercedes")
);

使用 print_r() 函數,我們將首先按原樣列印數組。

代碼:

//create multidimensional array
$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

輸出:

PHP 中的二維數組

現在我們將使用

為水果子數組添加一個元素
array_push() function

文法:

array_push(array, value1,value2...)

哪裡,

  • 數組是$input數組
  • value1 是要加入陣列的元素
  • value2、value3 可選

範例#1

代碼:

$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
array_push($input['colors'], "Black");
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

輸出:

PHP 中的二維數組

在下面的程式中,我們只是刪除了鍵“colors”,並發現它使用 0 鍵附加到給定數組的最後一個,如輸出圖像所示。

Example #2

Code:

// create multidimensional array
$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
// adding a value to array
array_push($input, "Black");
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

Output:

PHP 中的二維數組

Example #3

Code:

//create multidimensional array
$input = array (
array ("Red", "Green", "Blue"),
array ("Yellow", "Orange", "Purple")
);
//add a color to the array
array_push($input, "Black");
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

Output:

PHP 中的二維數組

How to Update Elements of 2D Arrays in PHP?

To update an element of the 2D array just get the key from the array and replace the value of that key in a particular array.

$input['cars']['Mercedes']  = 'Duster';

Example #1

Code:

//create multidimensional array
$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
//update the Mercedes with Duster
$input["cars"][2]  = "Duster";
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

Output:

PHP 中的二維數組

Example #2

Code:

//create multidimensional array
$input = array (
array ("Red", "Green", "Blue"),
array ("Yellow", "Orange", "Purple")
);
//update the Mercedes with Duster
$input[0][1] = "White";
// print the multidimensional array
echo "<pre class="brush:php;toolbar:false">";
print_r($input);
echo "<pre class="brush:php;toolbar:false">";

Output:

PHP 中的二維數組

How to Delete Elements of 2D Arrays?

To delete an element of the 2D array we will use array_shift() function.

array_shift removes and returns the first element value of the array.

Syntax:

array_shift(array)

where

-array is the $input array

Example #1

Code:

//create multidimensional array
$input = array (
"colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
//print the removed element
print_r(array_shift($input));

Output:

PHP 中的二維數組

Example #2

Code:

//create multidimensional array
$input = array (
array ("Red", "Green", "Blue"),
array ("Yellow", "Orange", "Purple")
);
//print the removed element
print_r(array_shift($input));

Output:

PHP 中的二維數組

Two dimensional in Associative Array

In the following example, we have created a 2-d array containing the information of books like the author of the book, type of book, and published in the year. Also, we will learn how to traverse or loop through this array. Looping through the multidimensional array we will use a nested foreach loop. Meaning one foreach loop inside another foreach loop. The same can also be done using for loop.

$input = array(
"The_Alchemist" => array (
"author" => "Paulo Coelho",
"type" => "Fiction",
"published_year" => 1988
),
"Managing_Oneself" => array (
"author" => "Peter Drucker",
"type" => "Non-fiction",
"published_year" => 1999
),
"Measuring_the_World" => array(
"author" => "Daniel Kehlmann",
"type" => "Fiction",
"published_year" => 2005
)
);

Just printing the above array without any loop will give us the following output:

Code:

// create multidimensional array
$input = array(
"The_Alchemist" => array (
"author" => "Paulo Coelho",
"type" => "Fiction",
"published_year" => 1988
),
"Managing_Oneself" => array (
"author" => "Peter Drucker",
"type" => "Non-fiction",
"published_year" => 1999
),
"Measuring_the_World" => array(
"author" => "Daniel Kehlmann",
"type" => "Fiction",
"published_year" => 2005
)
);
// print the plain multidimensional array
echo "
";
print_r($input);
echo "
";

Output:

PHP 中的二維數組

Now we will print the multidimensional array using a foreach loop.

Code:

// create multidimensional array
$input = array(
"The_Alchemist" => array (
"author" => "Paulo Coelho",
"type" => "Fiction",
"published_year" => 1988
),
"Managing_Oneself" => array (
"author" => "Peter Drucker",
"type" => "Non-fiction",
"published_year" => 1999
),
"Measuring_the_World" => array(
"author" => "Daniel Kehlmann",
"type" => "Fiction",
"published_year" => 2005
)
);
//foreach to loop the outer array
foreach($input as $book) {
echo "
"; // foreach to loop the inner array foreach($book as $key=>$value) { echo $key." ". $value. "
"; } }

Output:

PHP 中的二維數組

Conclusion

I hope this article is helpful to learn the concepts of the topic on a 2D array in PHP. This topic covers all the concepts required for the understanding related to the 2D array in PHP. This topic is made simpler with the help of examples with the output snapshots to refer to. According to the article, if all the programs are practiced well will surely help you to grasp the concepts easily. I hope the topic is made more informative for gaining more knowledge.

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

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