欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP數(shù)組實例詳解

 更新時間:2016年06月26日 14:39:49   作者:roucheng  
這篇文章主要介紹了PHP數(shù)組實例詳解的相關(guān)資料,數(shù)組分為數(shù)組數(shù)值數(shù)值,關(guān)聯(lián)數(shù)組,多維數(shù)組,本文介紹的非常不錯具有參考借鑒價值,需要的朋友可以參考下

作為一名C++程序員,在轉(zhuǎn)做PHP開發(fā)的過程中,對PHP數(shù)組產(chǎn)生了一些混淆,與C++數(shù)組有相似的地方,也有一些不同,下面就全面地分析一下PHP的數(shù)組及其與C++中相應(yīng)數(shù)據(jù)類型的區(qū)別和聯(lián)系。

數(shù)組的分類:

1、數(shù)值數(shù)組:也叫索引數(shù)組,即以數(shù)字(從0 開始)作為數(shù)組下標(biāo)。相當(dāng)于C++中的vector。

2、關(guān)聯(lián)數(shù)組:以字符串作為數(shù)組下標(biāo)。相當(dāng)于C++中的map。

3、多維數(shù)組:數(shù)組中每個元素也是一個數(shù)組。其子數(shù)組中的每個元素也可以是數(shù)組。

數(shù)組的聲明:

1、數(shù)值數(shù)組

a、如下例子中,會自動分配數(shù)字ID鍵。

$names = array("Peter","Joe","Lily");
b、如下例子中,我們?nèi)斯し峙鋽?shù)字ID鍵。

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

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

<?php 
$names[0] = "Peter"; 
$names[1] = "Joe"; 
$names[2] = "Lily"; 
echo $names[0]." and ".$names[1]." are ".$names[2]."'s neighbors"; 
/* 何問起 hovertree.com */
?> 

2、關(guān)聯(lián)數(shù)組:

例子1

$ages = array("Peter"=>32, "Joe"=>30, "Lily"=>28);

例子2

本例與例子1相同,只是另一種創(chuàng)建數(shù)組的方法。

$ages["Peter"] = "32"; 
$ages["Joe"] = "30"; 
$ages["Lily"] = "28"; 

在腳本中使用關(guān)聯(lián)數(shù)組:

<?php 
$ages["Peter"] = "32"; 
$ages["Joe"] = "30"; 
$ages["Lily"] = "28"; 
echo "Peter is ".$ages["Peter"]." years old."; 
/* 何問起 hovertree.com */
?> 

以上腳本輸出:

Peter is 32 years old.

3、多維數(shù)組:

在本例中,我們創(chuàng)建了一個帶有自動分配數(shù)字ID鍵的多維數(shù)組:

$families = array 
{ 
"Griffin"=>array 
{ 
"Peter", 
"Lois", 
"Megan" 
}, 
"Quagmire"=>array 
{ 
"Glenn" 
}, 
"Brown"=>array 
{ 
"Cleveland", 
"Loretta", 
"Junior" 
} 
}; 
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?"; 

以上代碼輸出:

Is Megan a part of the Griffin family?

1、for循環(huán)遍歷

for循環(huán)只能遍歷索引數(shù)組。

<?php 
$names = array("Peter","Joe","Lily"); 
for($id=0;$id<count($names);++$id) 
{ 
echo $names[$id]; 
} 
?> 

2、foreach遍歷

即可以遍歷索引數(shù)組,也可以遍歷關(guān)聯(lián)數(shù)組

遍歷索引數(shù)組

foreach(array_expression as $value) 
{ 
循環(huán)體; 
} 
遍歷關(guān)聯(lián)數(shù)組 
foreach(array_expression as $key=>$value) 
{ 
循環(huán)體; 
} 

A、一維數(shù)組遍歷

索引數(shù)組

<?php 
$contact = array("李某","xx公司","abc@xx.com"); 
foreach($contact as $value) 
{ 
echo $value; 
} 
?> 
<?php 
$contact = array("姓名"=>"李某","公司"=>"xx公司","郵箱"=>"abc@xx.com"); 
foreach($contact as $key=>$value) 
{ 
echo $key.":".$value; 
} 
?> 

B、多維數(shù)組遍歷

<?php 
$wage = array( 
"市場部"=array( 
array(1,"李某","市場經(jīng)理",8000), 
array(2,"王某","市場專員",5000), 
array(3,"劉某","市場專員",7000) 
), 
"產(chǎn)品部"=array( 
array(1,"李某","產(chǎn)品經(jīng)理",9000), 
array(2,"王某","產(chǎn)品專員",6000), 
array(3,"劉某","產(chǎn)品專員",5000) 
), 
"賬務(wù)部"=array( 
array(1,"李某","賬務(wù)經(jīng)理",7000), 
array(2,"王某","賬務(wù)專員",6000), 
array(3,"劉某","賬務(wù)專員",5000) 
) 
); 
foreach($wage as $section=>$table) 
{ 
echo $section."部門人員如下"; 
foreach($table as $row) 
{ 
foreach($row as $value) 
{ 
echo $value; 
} 
} 
} /* 何問起 hovertree.com */
?> 

以上所述是小編給大家介紹的php數(shù)組的實例詳解,希望對大家有所幫助。

相關(guān)文章

最新評論