一、什么是数组
数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体。数组的每个实体都包含两项:键和值。
二、声明数据
在PHP中声明数组的方式主要有两种:
一是应用array()函数声明数组,一是直接为数组元素赋值。
<1>array()函数声明的数组的方式array([mixed…]) 参数mixed的语法为key=>value,如下代码:
- <?php
- $array=array("1"=>"编","2"=>"程","3"=>"词","4"=>"典");
- print_r($array);
- echo "<br>";
- echo $array[1]; //注意:下标默认是从0开始
- echo $array[2];
- echo $array[3];
- echo $array[4];
- ?>
<2>直接为数组元素赋值。
如果在创建数组时不知所创建数组的大小,或在实际编写程序时数组的大小可能发生变化,采用这种数组创建的方法较好,如以下代码:
- <?php
- $array[1]="I";
- $array[2]="love";
- $array[3]="PHP";
- print_r($array); //输出所创建数组的结构
- ?>
三、数组的类型
PHP支持两种数组:索引数组(indexed array)和联合数组(associative array),前者使用数字作为键,后者使用字符串作为键。
四、输出数组
在PHP中对数组元素输出,可以通过echo和print语句来实现,但这只能对数组中某一元素进行输出;要将数组结构输出则要使用print_r()函数,其语法:print_r(mixed expression),参数expression为普通的整形、字符型或实型变量则输出该变量本身,如果该参数为数组,则按一定键值和元素的顺序显示出该数组中的所有元素。
五、数组的构造
一维数组:当一个数组的元素是变量时,则称其为一维数组。
声明一位数组: 类型说明符 数组名[常量表达式];
二维数组:当一个数组的元素是一个一位数组时,则称之为二维数组,代码如下:
- <?php
- $str = array (
- "网络编程语言"=>array ("PHP","JSP","ASP"),
- "体育项目"=>array ("m"=>"足球","n"=>"篮球"));
- print_r ( $str) ;
- ?>
六、遍历数组
遍历数组中的所有元素是常用的一种操作,在遍历的过程中可以完成查询或其他功能。在PHP中遍历数组的方法有多种,下面介绍最常用的两种方法。
<1>使用foreach结构遍历数组;
<2>使用list()函数遍历数组,list()函数仅能用于数字索引的数组,且数字索引从0开始。
例:综合使用list()和each()认证用户登录:
- <?php
- //输出用户登录信息
- while(list($name,$value)=each($_POST)){
- if($name!="submit"){
- echo "$name=$value<br>";
- }
- }
- ?>
七、统计数组元素个数
在PHP中,使用count()函数对数组中的元素个数进行统计,语法为:int coun(mixed array[,int mode]),其中参数array为必要参数,mode为可选参数,若选中COUNT——RECURSIVE(或1),本函数将递归地对数组数组,代码如下:
- <?php
- $array = array("php" => array("PHP函数参考大全","PHP程序开发范例宝典","PHP数据库教程系统开发完全手册"),
- "asp教程" => array("ASP经验技巧宝典")
- ); //声明一个二维数组
- echo count($array,COUNT_RECURSIVE);//递归统计数组元素的个数,运行结果为6
- ?>
八、数组排序
<1>使用sort()和rsort()分别对数组进行升序和降序,如,
- <?php
- $array=array(5,26,37,18,9,42,88,66);
- $array1=sort($array);
- for($i=0;$i<count($array);$i++){
- echo $array[$i]." ";
- }
- echo "<br>";
- $array1=rsort($array);
- for($i=0;$i<count($array);$i++){
- echo $array[$i]." ";
- }
- ?>
- //运行结果:
- //5 9 18 26 37 42 66 88
- //88 66 42 37 26 18 9 5
<2>使用ksort()和asort()对关联数组排序
如果使用了相关数组,在排序后还要保持关键字和值的排序一致,这是就需要使用ksort()和asort()函数,如下代码:
- <?php
- $array=array('php'=>1,'jsp'=>2,'asp'=>3);
- ksort($array);
- print_r($array);
- echo "<br>";
- asort($array);
- print_r($array);
- ?>
运行结果:
Array ( [asp] => 3 [jsp] => 2 [php] => 1 )
Array ( [php] => 1 [jsp] => 2 [asp] => 3 )
波比源码 » php数组的使用方法详解
levofloxacin canada order levaquin 500mg for sale
purchase avodart sale ondansetron drug zofran uk
purchase aldactone without prescription fluconazole 200mg cheap buy fluconazole 100mg online
ampicillin 250mg cost cephalexin 125mg tablet erythromycin 500mg us
suhagra order estrace usa order estradiol pills
lamotrigine 200mg without prescription buy mebendazole pills cheap tretinoin cream
order indomethacin order lamisil without prescription purchase trimox generic
anastrozole 1mg tablet Buy viagra viagra pills
tadalafil 10mg en france sildenafil 100mg comprimГ© viagra 100mg pour homme
order prednisone 10mg without prescription generic cialis 5mg daily sildenafil australia
cialis für frauen tadalafil 20mg für männer sildenafil kaufen ohne rezept
purchase accutane sale accutane 20mg cost ivermectin 500ml
order altace 10mg online cheap temovate order online purchase azelastine online cheap
order clonidine online cost catapres buy spiriva 9mcg online cheap
imdur 40mg cheap buy imuran for sale order telmisartan generic
cost bupropion bupropion 150mg cheap order quetiapine 100mg pill
molnupiravir 200 mg pills molnupiravir 200 mg canada buy prevacid 15mg without prescription
sertraline 50mg without prescription buy generic sertraline 100mg viagra 50mg usa
modafinil 100mg cost order provigil 200mg sale ivermectin for sale online
buy fluvoxamine 50mg sale fluvoxamine oral glucotrol 5mg us
order lasix 40mg for sale hydroxychloroquine pills plaquenil 200mg tablet
buy cialis 40mg sale order generic tadalafil 20mg anafranil 50mg ca
itraconazole pills order tindamax 500mg pills buy tinidazole 300mg online
olanzapine 10mg brand valsartan 160mg pills purchase valsartan online
clozaril cost cheap clozapine 100mg decadron 0,5 mg without prescription
viagra cheap viagra for women buy lisinopril 10mg pills
lopressor 100mg drug order lopressor 50mg generic vardenafil 20mg us
clomiphene 50mg us roulette online real money casino arizona
buy dapoxetine 90mg sale order synthroid pill levothyroxine over the counter
cialis on line viagra overnight shipping pfizer viagra
brand xenical 60mg orlistat 60mg pills zovirax medication
cheap cialis pill buy clopidogrel 150mg for sale order plavix sale
order generic zyloprim oral ezetimibe ezetimibe oral
order methotrexate order warfarin pill buy generic reglan 20mg
buy motilium 10mg without prescription generic tetracycline 500mg buy cyclobenzaprine 15mg
losartan 25mg oral topamax 100mg drug buy topiramate 200mg for sale
cheap imitrex 25mg buy levofloxacin 250mg pills order dutasteride generic
order colchicine 0.5mg for sale online gambling sites rivers casino
ranitidine 150mg pill order celecoxib 200mg generic buy celecoxib for sale
purchase flomax sale tamsulosin 0.4mg canada generic spironolactone
buy cialis 10mg without prescription tadalafil 10mg cheap ciprofloxacin 500mg canada
buy diflucan generic order diflucan 100mg without prescription real viagra sites
viagra 150mg order viagra pills tadalafil 20mg tablet
how to write an essay about my family buy stromectol 6mg stromectol ireland
online gambling tadalafil 20mg pill cialis without prescription
blackjack online play poker online free casino world buy provigil pill
buy retin cream online cheap avanafil 200mg price avana usa
order tadacip 10mg online buy diclofenac 50mg online cheap buy generic indocin 50mg
purchase terbinafine sale trimox 500mg pills buy amoxicillin generic
buy naproxen 500mg pill naproxen uk lansoprazole canada
order spiriva 9 mcg sale purchase terazosin pills terazosin 1mg oral
buy cialis 10mg sale ed pills gnc tadalafil 5mg usa
online gambling sites online casinos real money gambling games
stromectol 3mg amantadine 100mg pill cost avlosulfon 100mg
golden nugget online casino canada pharmaceuticals online write essay for money
ramipril 5mg for sale order altace pills cheap arcoxia 120mg
term paper service mba essay service azulfidine 500mg ca
buy generic asacol 800mg irbesartan price buy avapro online cheap
buy olmesartan generic order verapamil 240mg without prescription depakote 500mg without prescription
order clobetasol cost cordarone buy cordarone 200mg online cheap
order carvedilol for sale buy ditropan 5mg for sale generic elavil 50mg
buy pamelor 25 mg for sale pamelor 25mg price buy paroxetine pill
order famotidine 20mg generic buy remeron pill remeron 15mg ca
order fenofibrate without prescription buy sildenafil 100mg for sale viagra for women
purchase cialis generic Cialis by mail cheap sildenafil without prescription
glycomet brand cost glucophage tamoxifen 10mg price
modafinil 200mg oral promethazine generic buy promethazine 25mg generic
ed remedies lyrica over the counter purchase finasteride pills
buy zofran without prescription buy bactrim 960mg trimethoprim tablet
isotretinoin 10mg us zithromax 500mg usa order azithromycin 250mg generic
ventolin 2mg for sale synthroid over the counter purchase amoxiclav pills
prednisolone 5mg pill cost lasix 100mg buy lasix 40mg sale
provigil over the counter purchase lopressor sale order metoprolol 50mg generic
avodart 0.5mg pills purchase dutasteride online xenical cost
azathioprine 25mg us generic naprosyn cheap naproxen
dapsone 100 mg pill buy atenolol pills atenolol 100mg pill
viagra sildenafil 150mg cialis fast delivery buy generic cialis
order levaquin 500mg generic generic bupropion 150mg buy bupropion 150mg
buy cenforce pills for sale cenforce 50mg sale glycomet order online
where can i buy letrozole buy femara 2.5mg online cheap viagra 50 mg
order tadalafil 40mg pill cialis prices buy ed pills us
order neurontin 600mg online order vibra-tabs generic doxycycline 100mg pills
prednisolone 5mg brand prednisolone 20mg generic buy lasix tablets
buy doxycycline 100mg pills augmentin 375mg pill augmentin sale
buy orlistat 60mg buy zyloprim 300mg online buy allopurinol tablets
buy generic sumatriptan levaquin 250mg canada buy avodart 0.5mg online cheap
tegretol 400mg brand tegretol price purchase lincomycin online cheap
real cialis pharmacy prescription cialis online sildenafil mail order
buy generic duricef finasteride 1mg over the counter buy propecia 5mg without prescription
fluconazole 200mg drug ciprofloxacin drug buy ciprofloxacin
cheap estradiol 1mg prazosin price minipress without prescription
buy generic metronidazole 400mg order cephalexin 500mg for sale order cephalexin 500mg
brand nolvadex order rhinocort for sale buy ceftin generic
bimatoprost order online desyrel 50mg tablet cost desyrel 50mg
purchase minomycin without prescription purchase hytrin for sale pioglitazone 15mg cost
azithromycin 500mg without prescription prednisolone 20mg canada buy gabapentin online cheap
tadalafil 10mg without prescription tadalafil 20mg price cialis daily
furosemide online buy order albuterol online cheap antihistamine drugs list
ivermectin 3mg over the counter prednisone online buy buy prednisone 40mg without prescription
where can i buy levitra buy hydroxychloroquine 200mg online hydroxychloroquine 400mg for sale
buy benicar online cheap olmesartan online buy depakote canada
coreg pill order coreg 6.25mg pill order chloroquine 250mg online cheap
buy diamox online azathioprine online order order azathioprine 25mg pills
order lanoxin 250 mg lanoxin 250 mg generic purchase molnunat for sale
oral olumiant olumiant 2mg oral lipitor 20mg generic
order albuterol 100mcg sale proventil medication pyridium 200 mg tablet
order amlodipine 5mg sale how to get norvasc without a prescription treat esophagus