什么是数组?数组是一种编程结构,它是一个存储一组或一系列数值的变量,比如人口普查时对个人的身份登记,如姓名、性别、民族、出生等就可作为数组.
php中创建数组使用array()结构来定义,比如:
$people=array('name','sex','nation','brith');
而如何显示数组中的各元素的值,我们则是使用从0开始的索引,索引号在变量名称后的方括号中,比如:
- <?php
- $people=array('name','sex','nation','birth');
- echo $people[2];
- ?>
输出的$people[2]就是显示的是nation(索引第一项从0计数),php除了支持数字索引数组以外,还支持相关数组,所谓相关数组,就是可自定义关键字来替换不直观的数字索引,比如:
- <?php
- $peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');
- echo $peoples['cs'];
- ?>
使用相关数组使得输出的选择很直观(不需要预先计算索引号然后输出),定义的关键字和值之间使用“=>”符号定义.
根据php数组元素的两种显示方式,还可以如变量一样无需array()声明和初始化,直接自动创建数字,比如:
- $people[0]='name';
- $people[1]='sex';
- $people[2]='nation';
- $people[3]='brith';
- //或者
- $peoples['xm']='name';
- $peoples['xb']='sex';
- $peoples['mz']='nation';
- $peoples['cs']='birth';//该数组的大小根据所增加元素的多少动态的变化。
数组元素的显示:
在如上使用的无论$people[2]也好,无论$peoples['cs']也好,都只是输出已知的明确位置的数组元素值,如何快速输出所有或部分的数组元素,使用循环语句无疑是最快的方法:
- <?php
- $people=array('name','sex','nation','birth');
- for ($i=0;$i<4;$i++)
- echo "$people[$i] ";
- ?>
除了使用了解循环次数的for循环以外,还可以使用对循环次数无须要求的foreach语句:
- <?php
- $people=array('name','sex','nation','birth');
- foreach($people as $xiangmu)
- echo $xiangmu;
- ?>
$xiangmu变量将保存数组中的各元素值,依次显示出来,当然,为了输出的数据能间隔区分,可在数组元素之后输出空格:
echo $xiangmu." ";现在来看一款php从数组里筛选出重复的数据:
- <?php
- $num = count($array);
- if($num)
- {
- sort($array);
- }
- if($num!=0)
- {
- $m = $array[0];
- $n = 0;
- $kind=1;
- echo $array[0]."———-";
- for($z=0;$z<$num;$z++)
- {
- if($m!=$array[$z])
- {
- echo $array[$z-1]." ";
- echo $array[$z];
- $kind=0;
- $m=$array[$z];
- }
- $n++;
- $m++;
- //echo $array[$z]."'/n'";
- }
- if($kind==1)
- {
- echo $array[$num-1]."重复的数据! ";
- }
- ?>
波比源码 » php 数组的定义和初始化学习笔记
buy levofloxacin online buy levofloxacin 500mg generic
purchase avodart order flomax 0.2mg online zofran price
aldactone 25mg generic order spironolactone 25mg online order diflucan 200mg generic
brand acillin order erythromycin 250mg order erythromycin online
buy fildena online cheap methocarbamol usa buy robaxin 500mg generic
purchase suhagra pill estrace brand purchase estradiol generic
lamotrigine canada purchase retin online cheap buy retin cream generic
buy tadalafil 10mg online cheap tadalafil online buy order diclofenac 50mg pills
order isotretinoin 10mg generic amoxil 500mg zithromax brand
indocin drug buy trimox 250mg online cheap
cheap tadalafil sale Us pharmacy viagra viagra overnight delivery
cialis sans ordonnance en france acheter 20mg gГ©nГ©rique tadalafil en france vrai viagra 50mg prix
original tadalafil 10mg rezeptfrei sicher kaufen tadalafil generika sildenafil 200mg generika rezeptfrei kaufen
oral accutane 40mg order generic accutane 20mg stromectol ebay
order generic provigil 200mg cost provigil 200mg acetazolamide without prescription
altace for sale ramipril pills azelastine cost
buspar 10mg over the counter buy buspirone online oxybutynin order online
order benicar 10mg pills calan 240mg usa purchase diamox pills
tacrolimus drug labetalol 100 mg sale urso over the counter
buy isosorbide pill order micardis without prescription order micardis 80mg online cheap
buy bupropion online order seroquel 50mg online order seroquel 100mg
order zoloft 50mg online cheap brand zoloft 100mg order sildenafil 50mg pills
cialis us viagra for women in hindi sildenafil 100mg usa
buy cialis 40mg online order cialis amantadine canada
buy revia 50mg pill purchase revia pill aripiprazole cost
medroxyprogesterone 10mg sale purchase cyproheptadine pills order cyproheptadine 4mg generic
provigil price buy provigil sale stromectol for humans for sale
order nootropil viagra 50mg us overnight delivery viagra
buy generic azithromycin 250mg zithromax 500mg generic gabapentin cost
cialis overnight shipping usa tadalafil liquid sildenafil 50mg for sale
lasix 40mg pills buy furosemide online cheap plaquenil online order
sporanox 100mg oral sporanox 100 mg sale tinidazole generic
order metformin 500mg online cheap cialis 10mg for sale tadalafil uk
buy sildenafil 50mg online cheap buy sildenafil 50mg pills cost lisinopril 5mg
prilosec 20mg generic real money online blackjack best online gambling
buy metoprolol without prescription buy generic metoprolol 100mg order levitra 20mg
order an essay online real money spins online play poker online free
clomid 100mg pill purchase ventolin inhalator without prescription hollywood casino online
triamcinolone tablet aristocort 10mg canada order desloratadine 5mg online
tadalafil 20mg uk order inderal 10mg purchase clopidogrel generic
order allopurinol 100mg online crestor 20mg pills order generic ezetimibe 10mg
purchase motilium generic sumycin pills flexeril drug
buy generic cozaar losartan 50mg over the counter topamax 100mg canada
order sumatriptan purchase levaquin sale avodart online buy
flagyl 400mg cost sulfamethoxazole cheap purchase bactrim generic
diflucan 200mg for sale fluconazole for sale sildenafil dosage
buy cephalexin 250mg generic order erythromycin for sale erythromycin online order
buy cialis 5mg generic cialis 5mg usa viagra for men
cheap sildenafil for sale order generic sildenafil cialis drug
buy cefuroxime 250mg methocarbamol over the counter order robaxin online cheap
free slot play real money online casino buy cialis 10mg without prescription
college paper help paper writer ivermectin uk
order zithromax 500mg without prescription buy prednisolone 40mg generic buy gabapentin 100mg for sale
casino online roulette real money sugarhouse casino online order provigil
order furosemide 40mg for sale buy lasix pill plaquenil pills
order sildenafil 50mg pill budesonide cheap buy generic rhinocort
tadalafil 10mg brand brand indocin cost indomethacin 75mg
lamisil 250mg over the counter order lamisil generic trimox 500mg brand
female cialis tadalafil Buy cialis on line purchasing cialis on the internet
cialis from india free slots games blackjack poker online
nifedipine order buy allegra 180mg without prescription oral allegra 120mg
order diamox 250mg pills buy imdur 20mg online cheap imuran drug
buy digoxin pill telmisartan 20mg usa molnunat for sale
carvedilol without prescription oxybutynin 5mg us amitriptyline 50mg price
buy priligy 90mg generic order avanafil 200mg sale order motilium generic
pamelor 25mg over the counter buy paxil 20mg pills purchase paxil generic
indomethacin capsule cheap indocin 75mg cenforce price
famotidine 40mg generic order generic mirtazapine 30mg buy mirtazapine 30mg generic
purchase doxycycline without prescription buy aralen pills methylprednisolone 8 mg over counter
buy tricor generic tricor ca sildenafil in usa
nexium drug purchase furosemide online cheap furosemide oral
buy minocycline 50mg pill minocycline without prescription buy hytrin for sale
purchase provigil without prescription order stromectol generic buy promethazine generic
isotretinoin 20mg sale isotretinoin 20mg pills acillin cost
buy stromectol 12mg buy stromectol 12mg pill deltasone sale
zofran cheap zofran sale buy sulfamethoxazole pill
purchase accutane generic isotretinoin pills azithromycin 250mg cost
cheap prednisolone 10mg buy prednisolone generic generic furosemide 100mg
order modafinil generic buy metoprolol sale metoprolol us
buy avodart 0.5mg pill xenical usa buy orlistat
buy generic doxycycline 200mg order acyclovir 400mg online acyclovir 400mg us
buy azathioprine online cheap buy telmisartan for sale naprosyn 500mg drug
brand inderal 20mg buy carvedilol 25mg pills buy carvedilol pills
order oxybutynin 5mg online cheap oxcarbazepine for sale online oxcarbazepine 300mg ca
cefdinir online buy pantoprazole 40mg generic protonix 40mg without prescription
simvastatin order online buy zocor generic sildenafil pills
sildenafil 100mg sale free shipping viagra tadalafil 20mg us
phenergan pills modafinil 100mg price cheap tadalafil 40mg