PHP 中的數組

王林
發布: 2024-08-29 12:42:53
原創
582 人瀏覽過

下面的文章《PHP 中的陣列》提供了在 PHP 中建立陣列的概述。數組是類似資料類型的集合。數組在單一變數中儲存多個值。當變數也可以儲存值時,為什麼需要數組呢?答案是因為儲存有限資料的值(例如數字5的計數)是可能的,但是當計數增加到例如100或200時,我們需要在100個變數中儲存100個值,這有點困難;因此,我們將其儲存在一個數組中。這就是使用數組的原因。

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

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

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

如何在 PHP 中建立陣列?

文法:

variablename = array();
登入後複製

variablename[i] = value;
登入後複製

其中變數名稱是變數的名稱,i是鍵,或索引值是元素值。

建立陣列的範例

$colors = array("Red","Green","Blue");
登入後複製

為了計算陣列的長度,我們使用 count 關鍵字。

$length = count($colors); // output is 3
登入後複製

數組中的每個值稱為數組的一個元素。數組索引從0開始。數組中最後一個元素的索引是數組總長度減1。

在上面給出的範例中,Red 的索引為 0,Green 為 1,Blue 為 2。因此,借助索引或鍵來存取數組變得更加容易。為了取得數組每個索引處的值,我們循環遍歷給定的陣列。要循環數組,我們使用 foreach 迴圈或 for 迴圈。

數組在 PHP 中如何運作?

像 foreach 和 for 這樣的迴圈用於迴圈遍歷數組。每個數組都有從 0 開始的索引,依此類推:

PHP 中的陣列類型

PHP中有三種類型的陣列;讓我們詳細了解每種類型的陣列:

  1. 數字或索引數組
  2. 關聯數組
  3. 多維數組
1.數值數組

在此數組類型中,索引始終是數字,它不能是字串。相反,它可以儲存任意數量的元素和任意類型的元素。

文法:

variable name = array("value1","value2","value3","value4")
登入後複製

代碼:

<?php
//Example to demonstrate numeric array
$input = array("Apple", "Orange", "Banana", "Kiwi");
//Here, to get these values we will write like
echo $input[0] . "\n"; // will give Apple
echo $input[1] . "\n"; // will give Orange
echo $input[2] . "\n"; // will give Banana
echo $input[3] . "\n"; // will give Kiwi
// To get the length of array we will use count
echo "The count of the array is " . count($input); // will give 4
echo "\n";
//To print the array we can use
print_r($input);
?>
登入後複製

輸出:

PHP 中的數組

宣告數值陣列的另一種方法是以下程式。在這個程式中,我們還將看到修改和列印值。

代碼:

<?php
//Example to demonstrate numeric array in another way
$input[0] = "Apple";
$input[1] = "Orange";
$input[2] = "Banana";
$input[3] = "Kiwi";
// To get Kiwi we will write like
echo $input[3]."<br>"; // will give Kiwi
//To modify Orange value
$input[1] = "Mango";
// Now echo $input[1] will give Mango
echo $input[1]."<br>"; // Mango
//To print the array we can use
print_r($input);
?>
登入後複製

輸出:

PHP 中的數組

現在我們將學習如何使用for迴圈來遍歷陣列

代碼:

<?php
//Example to demonstrate for loop on a numeric array
//declaring the array
$input = array("Apple", "Orange", "Banana", "Kiwi", "Mango");
//the for loop to traverse through the input array
for($i=0;$i<count($input); $i++) {
echo $input[$i];
echo "<br>";
}
?>
登入後複製

輸出:

PHP 中的數組

2.關聯數組

這個陣列是鍵值對的形式,其中鍵是陣列的索引,值是陣列的元素。

文法:

$input = array("key1"=>"value1",
"key2"=>"value2",
"key3"=>"value3",
"key4"=>"value4");
登入後複製

不使用陣列關鍵字宣告關聯陣列的另一種方法

$input[$key1] = $value1;
$input[$key2] = $value2;
$input[$key3] = $value3;
$input[$key4] = $value4;
登入後複製

代碼:

<?php
//Example to demonstrate associative array
//declaring an array
$input = array(
"Jan"=>31,
"Feb"=>28,
"Mar"=>31,
"Apr"=>30);
// the for loop to traverse through the input array
foreach($input as $in) {
echo $in."<br>";}
?>
登入後複製

輸出:

PHP 中的數組

3.多維數組

這個數組是數組的數組,其中數組的值包含數組。

文法:

$input =array(
array('value1', 'value2', 'value3'),
array('value4', 'value5', 'value6'),
array('value7', 'value8', 'value9'));,
登入後複製

代碼:

<?php
//Example to demonstrate multidimensional array
// declaring a multidimensional array
$input = array ("colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
//the foreach loop to traverse through the input array
foreach($input as $key=>$value) {
echo $key .'--'. "<br>";
foreach($value as $k=>$v)
{echo $v ." ";}
echo "<br>";
}
?>
登入後複製

輸出:

PHP 中的數組

關聯數組中的多維數組

代碼:

<?php
//Example to demonstrate multidimensional array
// declaring a 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
));
//the foreach loop to traverse through the input array
//foreach to loop the outer array
foreach($input as $book) {
echo "<br>";
// foreach to loop the inner array
foreach($book as $key=>$value)
{
echo $key." ". $value. "<br>";}
}?>
登入後複製

輸出:

PHP 中的數組

PHP 中的陣列方法

以下是 PHP 中陣列的方法:

1. Count() 方法

此方法用於統計數組中元素的數量。

文法:

Count(array, mode)
登入後複製

需要計數的地方,模式是可選的。

代碼:

<?php
//Example to demonstrate use of in_array method
//declaring associative array
$input=array('English','Hindi','Marathi');
//counting the number of elements in the given array
echo count($input);
?>
登入後複製

輸出:

PHP 中的數組

2. Array_walk() 方法

此方法接受兩個參數作為輸入;第一個參數是輸入數組,第二個參數是宣告的函數的名稱。此方法用於循環遍歷數組中的每個元素。

文法:

array_walk(array, function_name, parameter...)
登入後複製

其中需要陣列的地方需要 function_name

參數是可選的

代碼:

<?php
//Example to demonstrate use of array_walk method
//creating a function to print the key and values of the given array
function fun($val, $k) {
echo $k. " --" .$val ."\n";
}
// declaring associative array
$input=array("e"=>'English', "h"=>'Hindi', "m"=>'Marathi');
//passing this array as a first parameter to the function
// array_walk,
//second paramter as the name of the function being called
array_walk($input,"fun");
?>
登入後複製

Output:

PHP 中的數組

3. In_array() method

This method performs a search on the array, whether the given array contains a particular value or not. If found or not found, it will execute respective if, else block

Syntax:

in_array(search_value, array_name)
登入後複製

Where both the parameters are required

Code:

<?php
//Example to demonstrate use of in_array method
// declaring associative array
$input=array('English','Hindi','Marathi', "Maths", "Social Science");
// using in_array to find Maths in given array
if(in_array("Maths", $input)) {
echo "Found Maths in the given array";
}
else
{
echo "Did not find Maths in the given array";
}
?>
登入後複製

Output:

PHP 中的數組

4. Array_pop() method

This method removes the last element from the given array.

Syntax

array_pop(array_name)
登入後複製

Code:

<?php
//Example to demonstrate use of array_pop method
// declaring array
$input=array('English','Hindi','Marathi');
// before using array_pop on the given array
print_r($input);
// after using array_pop method on the given array
array_pop($input);
echo "\n ";
print_r($input);
?>
登入後複製

Output:

PHP 中的數組

5. Array_push() method

This method adds given elements at the end of the array.

Syntax:

array_push(array_name, value1, value2, ...)
登入後複製

Code:

<?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English','Hindi','Marathi');
// before using array_push on the given array
print_r($input);
// after using array_push method on the given array
array_push($input, "Economics", "Maths", "Social Science");
echo "\n";
//printing the array
print_r($input);
?>
登入後複製

Output:

PHP 中的數組

6. Array_shift() method

This method removes and returns the first element of the array.

Syntax: 

array_shift(array_name)
登入後複製

Code:

<?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English','Hindi','Marathi');
// before using array_shift on the given array
print_r($input);
echo "\n";
// after using array_shift method on the given array
echo array_shift($input);
?>
登入後複製

Output:

PHP 中的數組

7. Array_unshift() method

This method inserts given elements into the beginning of the array.

Syntax:

array_unshift(array_name, value1, value2,…)
登入後複製

Code:

<?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English','Hindi','Marathi');
// before using array_unshift on the given arrayprint_r($input);
echo "\n";
// after using array_unshift method on the given array
array_unshift($input, "Economics");
print_r($input);
?>
登入後複製

Output:

PHP 中的數組

8. Array_reverse() method

This method is used to reverse the elements of the array.

Syntax:

array_reverse(array_name, preserve)
登入後複製

where array_name is required,

preserve is optional

Code:

<?php
//Example to demonstrate use of in_array method
// declaring associative array
$input=array("e"=>'English',"h"=>'Hindi',"m"=>'Marathi');
// array before reversing the elements
print_r($input);
echo "\n";
// printing the reverse
// array after reversing the elements
print_r(array_reverse($input));
?>
登入後複製

Output:

PHP 中的數組

Conclusion

This article covers all levels of concepts, simple and complex, of the topic arrays in PHP. I hope you found this article interesting and informative for the learning purpose.

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

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