一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

PHP之數組學習

今天學習了數組,可以說是php的數據應用中較重要的一種方式。php的數組函數眾多,下面是我學習的小結,借此記之,便于以后鑒之……
  一、數組定義:
  數組的定義使用 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

php技術PHP之數組學習,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 色多多入口 | 国产 欧美 日产中文 | 成人免费视频网站 | 欧美高清狂热视频60一70 | 亚洲视频黄色 | 精品在线91| 成人年鲁鲁在线观看视频 | 91福利国产在线在线播放 | 国产成人综合久久亚洲精品 | 欧美一级特黄做 | 91大神免费视频 | 国产美女视频免费 | 2021精品国夜夜天天拍拍 | 四虎永久在线精品视频免费观看 | 伊人色综合网一区二区三区 | 91精彩视频在线观看 | 国产在线观看黄色 | 美女国内精品自产拍在线播放 | 999成人精品视频在线 | 欧美人与禽交视频在线观看 | 精品福利一区二区免费视频 | 国产日韩欧美不卡www | 色优影院 | 碰91精品国产91久久婷婷 | 国产综合自拍 | 国产全部理论片线观看 | 成 人 黄 色 视频播放165 | 亚洲日本一区二区三区高清在线 | 亚洲一区二区三区麻豆 | 午夜欧美成人久久久久久 | 亚洲狠狠狠一区二区三区 | 久久99国产精品久久99软件 | 国产精品一区二区久久不卡 | 美女隐私视频黄www视频 | 久久久久久久久久久96av | 成人a在线| 欧美一区二区自偷自拍视频 | 久久免费精品一区二区 | 97碰视频人人做人人爱欧美 | 激情都市亚洲 | 美女毛片免费看 |