有很多朋友都不知道怎么把数组中元素给删除,下面我来总结各种数组删除元素方法给各位.
删除数组指定元素,代码如下:
- <?php
- $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
- print_r(array_slice($a,1,2));
- ?>
- //输出
- Array ( [0] => Cat [1] => Horse )
几个函数用下来觉得array_search()比较实用.
array_search() 函数与 in_array() 一样,在数组中查找一个键值,如果找到了该值,匹配元素的键名会被返回,如果没找到,则返回 false,代码如下:
- $array = array('1', '2', '3', '4', '5');
- $del_value = 3;
- unset($array[array_search($del_value , $array)]);//利用unset删除这个元素
- print_r($array);
- 输出
- array('1', '2', '4', '5');
从数组头删除值
array_shift()函数删除并返回数组中找到的元素,其结果是,如果使用的是数值健,则所有相应的值都会下移,而使用关联键的数组不受影响,其形式为:
mixed array_shift(array array)
下面的例子删除了$fruits数组中的第一个元素apple,代码如下:
- $fruits = array("apple","banana","orange","pear");
- $fruit = array_shift($fruits);
- // $fruits = array("banana","orange","pear")
- // $fruit = "apple";
从数组尾删除元素
array_pop()函数删除并返回数组的最后一个元素,其形式为:
mixed array_pop(aray target_array);
下面的例子从$states数组删除了最后的一个州,代码如下:
- $fruits = array("apple","banana","orange","pear");
- $fruit = array_pop($fruits);
- //$fruits = array("apple","banana","orange");
- //$fruit = "pear";
删除数组中间的元素,代码如下:
- function deleteElementFromArr($arr, $index){
- if($index < count($arr)-1){
- unset($arr[$index]);
- reset($arr);
- }
- return $arr;
- }
我封装成了一个函数,方便大家使用,代码如下:
- <?php
- function array_remove(&$arr, $offset)
- {
- array_splice($arr, $offset, 1);
- }
- $arr = array('apple','banana','cat','dog');
- array_remove($arr, 2);
- print_r($arr);
- ?>
删除数组重复元素,代码如下:
- <?php
- function assoc_unique($arr, $key) {
- $tmp_arr = array();
- foreach($arr as $k => $v) {
- if(in_array($v[$key], $tmp_arr)) {
- unset($arr[$k]);
- } else {
- $tmp_arr[] = $v[$key];
- }
- }
- sort($arr);
- return $arr;
- }
- $aa = array(
- array('id' => 123, 'name' => '张三'),
- array('id' => 123, 'name' => '李四'),
- array('id' => 124, 'name' => '王五'),
- array('id' => 125, 'name' => '赵六'),
- array('id' => 126, 'name' => '赵六')
- );
- $key = 'name';
- assoc_unique(&$aa, $key);
- print_r($aa);
- ?>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php数组删除元素各种方法总结
波比源码 » php数组删除元素各种方法总结
order levofloxacin online cheap purchase levofloxacin pill
dutasteride for sale online order zofran generic buy ondansetron generic
brand spironolactone brand fluconazole 200mg cheap diflucan 100mg
ampicillin pills ampicillin 250mg ca erythromycin 250mg for sale
sildenafil 50mg drug bimatoprost online order methocarbamol 500mg cheap
buy suhagra 50mg without prescription order estrace 1mg sale estradiol 1mg cost
lamictal buy online purchase mebendazole online cheap order generic tretinoin gel
buy tadalis sale oral avanafil 200mg diclofenac 50mg drug
buy isotretinoin 40mg without prescription purchase amoxicillin generic buy zithromax 500mg online
oral indocin 75mg trimox 250mg without prescription buy amoxicillin for sale
isotretinoin 10mg brand stromectol humans stromectol cost in usa
order doxycycline online cheap doxycycline 100mg cost furosemide for sale
catapres cheap order tiotropium bromide generic order spiriva 9mcg generic
order hytrin 5mg pill buy actos 15mg without prescription order azulfidine 500mg without prescription
fosamax 70mg uk paracetamol generic famotidine price
olmesartan without prescription calan 240mg pill order diamox for sale
buy isosorbide 20mg pill oral imuran 50mg order micardis 20mg for sale
buy molnupiravir 200 mg generic naprosyn 250mg brand order prevacid pill
tadalafil order Generic cialis viagra 50mg over the counter
cheap cialis 10mg order pyridium pills order generic symmetrel
avlosulfon ca aceon 8mg tablet order perindopril 4mg without prescription
buy medroxyprogesterone pill order biltricide 600 mg online cheap cyproheptadine drug
cost provigil brand stromectol 6mg stromectol price uk
order luvox 50mg online purchase glipizide without prescription generic glipizide 10mg
isotretinoin tablet prednisone 10mg over the counter brand deltasone 10mg
piracetam 800 mg for sale viagra 50mg ca sildenafil 50mg uk
buy azithromycin 500mg pills prednisolone 20mg brand neurontin order online
cialis 10mg uk buy generic viagra 100mg viagra 100 mg
lasix 40mg pill buy hydroxychloroquine 200mg without prescription hydroxychloroquine 200mg pills
chloroquine uk purchase cenforce without prescription order baricitinib 2mg without prescription
clozaril brand brand clozapine 100mg order decadron 0,0,5 mg online
viagra overnight sildenafil pills 25mg purchase lisinopril pills
purchase linezolid generic buy linezolid 600 mg online cheap play blackjack
prilosec 10mg sale buy prilosec online casino moons online casino
buy metoprolol vardenafil without prescription vardenafil 20mg brand
vardenafil us can i buy methylprednisolone online medrol 4 mg tablet
buy dapoxetine 90mg order priligy 60mg generic synthroid order
order cialis 40mg generic sildenafil 200 mg buy viagra 100mg pills
orlistat order diltiazem cost zovirax 400mg pill
cost domperidone 10mg brand cyclobenzaprine cyclobenzaprine cheap
losartan 25mg brand buy topiramate generic order topamax 200mg without prescription
order zantac 300mg ranitidine cost purchase celebrex for sale
slot games free best online poker sites for real money casino games free
flomax 0.4mg pill brand tamsulosin spironolactone price
purchase zocor sale brand zocor 10mg finasteride 5mg for sale
metronidazole 400mg generic augmentin 375mg uk sulfamethoxazole ca
purchase cefuroxime for sale order methocarbamol 500mg for sale brand methocarbamol 500mg
academicwriters buy stromectol 12mg ivermectin 12mg pills
sildalis us sildenafil dosage order lamictal 200mg
azithromycin tablet zithromax 500mg drug generic gabapentin 800mg
furosemide 100mg cost doxycycline usa hydroxychloroquine oral
buy fildena sale nolvadex ca rhinocort sale
buy tretinoin gel online cheap order avana 200mg sale purchase avana generic
terbinafine 250mg brand oral terbinafine 250mg amoxicillin 250mg usa
purchase naprosyn generic lansoprazole 15mg oral lansoprazole us
buy albuterol 100mcg order generic pantoprazole 40mg buy cipro 500mg for sale
ivermectin 1 cream generic symmetrel 100mg cost dapsone 100mg generic
online roulette free ocean casino online slots casino
doxycycline 100mg uk generic doxycycline 100mg cleocin uk
mesalamine 800mg cost buy astelin 10ml online irbesartan 300mg pill
generic carvedilol 25mg carvedilol 6.25mg ca buy elavil 50mg for sale
order fosamax sale purchase fosamax online cheap brand ibuprofen 600mg
pepcid 20mg generic purchase famotidine online cheap order mirtazapine without prescription
order tricor pills tricor usa viagra 100mg without prescription
esomeprazole capsules clarithromycin us lasix order
modafinil 100mg cost stromectol 6mg ca cheap promethazine 25mg
glycomet 1000mg pills order verapamil 240mg pills nolvadex 20mg pills
prednisone 10mg us prednisone 10mg over the counter buy amoxicillin 500mg without prescription
order clomiphene 100mg generic order prednisolone 10mg without prescription order prednisolone 40mg generic
fildena order cheap pregabalin buy propecia 5mg pill
isotretinoin 20mg canada ampicillin online buy ampicillin 250mg canada
order zofran 4mg without prescription buy amoxicillin 500mg pills sulfamethoxazole cost
ivermectin 6mg for sale buy stromectol online buy generic deltasone
accutane cost zithromax medication azithromycin 250mg for sale