萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php數組各種應用與用法

php數組各種應用與用法

什麼是數組?
在使用 PHP 進行開發的過程中,或早或晚,您會需要創建許多相似的變量。

無需很多相似的變量,你可以把數據作為元素存儲在數組中。

數組中的元素都有自己的 ID,因此可以方便地訪問它們。

有三種數組類型:
數值數組
帶有數字 ID 鍵的數組
關聯數組
數組中的每個 ID 鍵關聯一個值
多維數組
包含一個或多個數組的數組 數值數組
數值數組存儲的每個元素都帶有一個數字 ID 鍵。

可以使用不同的方法來創建數值數組:

例子 1
在這個例子中,會自動分配 ID 鍵:

$names = array("Peter","Quagmire","Joe");例子 2
在這個例子中,我們人工分配的 ID 鍵:

$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";

可以在腳本中使用這些 ID 鍵:

<?php教程

$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";

echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors";
?>
以上代碼的輸出:

Quagmire and Joe are Peter's neighbors

關聯數組
關聯數組,它的每個 ID 鍵都關聯一個值。

在存儲有關具體命名的值的數據時,使用數值數組不是最好的做法。

通過關聯數組,我們可以把值作為鍵,並向它們賦值。

例子 1
在本例中,我們使用一個數組把年齡分配給不同的人:

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);例子 2
本例與例子 1 相同,不過展示了另一種創建數組的方法:

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
可以在腳本中使用 ID 鍵:

<?php

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";
?>

以上腳本的輸出:

Peter is 32 years old.多維數組
在多維數組中,主數組中的每個元素也是一個數組。在子數組中的每個元素也可以是數組,以此類推。

例子 1
在本例中,我們創建了一個帶有自動分配的 ID 鍵的多維數組:

$families = array
(
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
);

如果輸出這個數組的話,應該類似這樣:

Array
(
[Griffin] => Array
  (
  [0] => Peter
  [1] => Lois
  [2] => Megan
  )
[Quagmire] => Array
  (
  [0] => Glenn
  )
[Brown] => Array
  (
  [0] => Cleveland
  [1] => Loretta
  [2] => Junior
  )
)

例子 2
讓我們試著顯示上面的數組中的一個單一的值:

echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";
以上代碼的輸出:

Is Megan a part of the Griffin family?

一、數組定義:
  數組的定義使用 array()方式定義,可以定義空數組:
 代碼如下:

<?php
$number = array(1,3,5,7,9);
//定義空數組
$result = array();
$color =array("red","blue","green");
//自定義鍵值
$language = (1=>"English",3=>"Chinese",5=>"Franch");
//定義二維數組
$two = array(
"color"=>array("red","blue"), //用逗號結尾
"week"=>array("Monday","Friday") //最後一句沒有標點
);
?>

二、創建數組:
  創建數組包含的函數有compact()、
    1.compact()函數——將一個或多個變量(包含數組)轉換為數組:
      array compact ( mixed $varname [, mixed $... ] )  
 代碼如下:

<?PHP
$number = "1,3,5,7,9";
$string = "I'm PHPer";
$array = array("And","You?");
$newArray = compact("number","string","array");
print_r ($newArray);
?>

compact()函數用於將兩個或多個變量轉換為數組,當然也包含數組變量。其參數是變量的名稱而非帶有$全名。
      相反的函數是extract()作用顧名思義就是將數組轉換為單個的字符串,鍵值作為其字符串名稱,數組值作為字符串的值。
      運行結果:
 代碼如下:

Array ( [number] => 1,3,5,7,9 [string] => I'm PHPer [array] => Array ( [0] => And [1] => You? ) )

2.array_combine()——將兩個數組重組成一個數組,一個作鍵值一個做的值:
      array array_combine ( array $keys , array $values )
 代碼如下:

<?PHP
$number = array("1","3","5","7","9");
$array = array("I","Am","A","PHP","er");
$newArray = array_combine($number,$array);
print_r ($newArray);
?>

array_combine函數不多說了,誰看了都明白
      運行結果:

Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )
    3.range()函數——創建指定范圍的數組:
       不多說了,直接上實例——
 代碼如下:

<?PHP
$array1 = range(0,100,10);//0為起始值,100為結束值,10為步進值(默認步進值為1).
print_r($array1);
echo"<br />";
$array2 = range("A","Z");
print_r($array2);
echo "<br />";
$array3 = range("z","a");
print_r($array3);
?>

range()函數的默認步進值是1!
      運行結果:
 代碼如下:

Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 )
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M [13] => N [14] => O [15] => P [16] => Q [17] => R [18] => S [19] => T [20] => U [21] => V [22] => W [23] => X [24] => Y [25] => Z )
Array ( [0] => z [1] => y [2] => x [3] => w [4] => v [5] => u [6] => t [7] => s [8] => r [9] => q [10] => p [11] => o [12] => n [13] => m [14] => l [15] => k [16] => j [17] => i [18] => h [19] => g [20] => f [21] => e [22] => d [23] => c [24] => b [25] => a )

4.array_fill()函數——填充數組函數:
 代碼如下:

<?PHP
$array = range(1,10);
$fillarray = range("a","d");
$arrayFilled = array_fill(0,5,$fillarray);//這裡的$fillarray可以是字符串,如"test".
echo "<pre>";
print_r ($arrayFilled);
echo "</pre>";
$keys = array("string","2",9,"SDK","PK");
$array2 = array_fill_keys($keys,"testing");
echo "<pre>";
print_r ($array2);
echo "</pre>";
?>

運行結果:
 代碼如下:

Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[2] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[3] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[4] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
)
Array
(
[string] => testing
[2] => testing
[9] => testing
[SDK] => testing
[PK] => testing
)

二、數組的遍歷:
 

   1.foreach遍歷:
       foreach (array_expression as $value){}
       foreach (array_expression as $key => $value){}
      閒話少說,上實例:
 代碼如下:
<?PHP
$speed = array(50,120,180,240,380);
foreach($speed as $keys=>$values){
echo $keys."=>".$values."<br />";
}
?>

運行結果:
 代碼如下:
0=>50
1=>120
2=>180
3=>240
4=>380

2.while循環遍歷:
      while循環遍歷一般結合list函數,以下是實例
 代碼如下:
<?PHP
$staff = array(
array("姓名","性別","年齡"),
array("小張","男",24),
array("小王","女",25),
array("小李","男",23)
);
echo "<table border=2>";
while(list($keys,$value) = each($staff)){
list($name,$sex,$age) = $value;
echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";
}
echo "</table>";
?>

運行結果:
姓名 性別 年齡
小張 男 24
小王 女 25
小李 男 23
3.for循環遍歷:
 代碼如下:
<?PHP
$speed = range(0,220,20);
for($i =0;$i<count($speed);$i++) {
echo $speed[$i]." ";
}
?>

運行結果:
 代碼如下:
0 20 40 60 80 100 120 140 160 180 200 220

copyright © 萬盛學電腦網 all rights reserved