1、建立乙個空陣列:
$result= array();
print_r($result);
輸出結果: array()
2、建立,初始化陣列:
$result= array("and","you ");
print_r($result);
輸出結果: array ( [0] => and [1] => you )
3、陣列compact
$number = "1,3,5,7,9";
$string = "i'm phper";
$array = array("and","you?");
$newarray = compact("number","string","array");
print_r ($newarray);
輸出結果: array ( [number] => 1,3,5,7,9 [string] => i'm phper [array] => array ( [0] => and [1] => you? ) )
4、array_combine()
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 ( [1] => i [3] => am [5] => a [7] => php [9] => er )
5、 range()函式——建立指定範圍的陣列:
$array1 = range(0,100,10);//0為起始值,100為結束值,10為步進值(預設步進值為1).
print_r($array1);
echo"
"; $array2 = range("a","z");
print_r($array2);
echo "
"; $array3 = range("z","a");
print_r($array3);
輸出結果:
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 )
6、array_fill()
array_fill()函式——填充陣列函式:
$array = range(1,10);
$fillarray = range("a","d");
$arrayfilled = array_fill(0,5,$fillarray);//這裡的$fillarray可以是字串,如"test".(0,5) 0-4 相當於key值
echo "
";";print_r ($arrayfilled);
echo "
$keys = array("string","2",9,"sdk","pk");
$array2 = array_fill_keys($keys,"testing");
echo "
";";print_r ($array2);
echo "
輸出結果:
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
)
7、陣列的遍歷
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)
?>
0=>50
1=>120
2=>180
3=>240
4=>380
另一種方法:
$speed = array(50,120,180,240,380);
foreach($speed as $keys=>$values)
輸出結果:
50 120
180
240
380
8、//for 迴圈遍歷陣列
//宣告乙個陣列並且初始化
$array=array('google','chrome','android','youtube','gmail');
//使用for迴圈遍歷各陣列元素,count()用來計算陣列的長度
for($i=0;$i//列印陣列的元素的值
echo $array[$i],"
";
PHP 陣列詳解
陣列能夠在單個變數中儲存多個值 cars array volvo bmw toyota echo i like cars 0 cars 1 and cars 2 陣列是乙個能在單個變數中儲存多個值的特殊變數。如果您有乙個專案清單 例如 車名字的清單 將其儲存到單個變數中,如下所示 cars1 vol...
PHP陣列詳解
作為一名c 程式設計師,在轉做php開發的過程中,對php陣列產生了一些混淆,與c 陣列有相似的地方,也有一些不同,下面就全面地分析一下php的陣列及其與c 中相應資料型別的區別和聯絡。1 數值陣列 也叫索引陣列,即以數字 從0 開始 作為陣列下標。相當於c 中的vector。2 關聯陣列 以字串作...
PHP陣列用處 PHP陣列操作詳解
概述 要訪問乙個變數的內容,可以直接使用其名稱。如果該變數是乙個陣列,可以使用變數名稱和關鍵字或索引的組合來訪問其內容。像其他變數一樣,使用運算子 可以改變陣列元素的內容。陣列單元可以通過 array key 語法來訪問。陣列的基本操作 php定義陣列 array array array key v...