方法如下:
- $arr = array(…………) ;//假设有一万个元素的数组,里面有重复的元素。
- $arr = array_flip(array_flip($arr)); //这样便可以删除重复元素。
究竟是怎么回事呢?来看下array_flip()的作用:array_flip()用于将一个数组的每个元素的键和值交换,如:
- $arr1 = array (”age” => 30, “name” => “快乐园”);
- $arr2 = array_flip($arr1); //$arr2 就是 array(30 => “age”, “快乐园” => “name”);
在php的数组中,允许不同的元素可以取同一个值,但不允许同一个键名被不同的元素使用,如:
- $arr1 = array (”age” => 30, “name” => “快乐园”, “age” => 20); “age” => 20将会取代”age” => 30
- $arr1 = array (”name” => “快乐园”, “age” => 45);
这里 $arr1与$arr2是相等的,于是,我们便可以知道,为什么array_flip(array_flip($arr))可以删除数组中重复的元素了,首先,$arr里的值会变成键名,因为值是有重复的,变成键名之后这些重复的值便成了重复的键名,PHP引擎将重复的键名删除,只保留最后一个。如:
- $arr1 = array (”age” => 30, “name” => “快乐园”, “age” => 20);
- $arr1 = array_flip($arr1); //$arr1 变成了 array(”快乐园” => “name”, 20 => “age”);
- //再把 $arr1 的键名与值还复:
- $arr1 = array_flip($arr1);
上面的代码写得简洁一些就是:$arr1 = array_flip(array_flip($arr1));
上面写了一篇关于数组去重的文章,但是仅限于一维数组,下面的函数可以用于二维数组:
- //二维数组去掉重复值
- function array_unique_fb($array2D)
- {
- foreach ($array2D as $v)
- {
- $v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
- $temp[] = $v;
- }
- $temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
- foreach ($temp as $k => $v)
- {
- $temp[$k] = explode(",",$v); //再将拆开的数组重新组装
- }
- return $temp;
- }
如果你想保留数组的键值,可以用下面的函数:
- //二维数组去掉重复值 并保留键值
- function array_unique_fb($array2D)
- {
- foreach ($array2D as $k=>$v)
- {
- $v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
- $temp[$k] = $v;
- }
- $temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
- foreach ($temp as $k => $v)
- {
- $array=explode(",",$v); //再将拆开的数组重新组装
- $temp2[$k]["id"] =$array[0];
- $temp2[$k]["litpic"] =$array[1];
- $temp2[$k]["title"] =$array[2];
- $temp2[$k]["address"] =$array[3];
- $temp2[$k]["starttime"] =$array[4];
- $temp2[$k]["endtime"] =$array[5];
- $temp2[$k]["classid"] =$array[6];
- $temp2[$k]["ename"] =$array[7];
- }
- return $temp2;
- }
大概就这样啦,二维数组去重复
- <?php
- $arr = array(
- array('id' => 1,'name' => 'aaa'),
- array('id' => 2,'name' => 'bbb'),
- array('id' => 3,'name' => 'ccc'),
- array('id' => 4,'name' => 'ddd'),
- array('id' => 5,'name' => 'ccc'),
- array('id' => 6,'name' => 'aaa'),
- array('id' => 7,'name' => 'bbb'),
- );
- function assoc_unique(&$arr, $key)
- {
- $rAr=array();
- for($i=0;$i<count($arr);$i++)
- {
- if(!isset($rAr[$arr[$i][$key]]))
- {
- $rAr[$arr[$i][$key]]=$arr[$i];
- }
- }
- $arr=array_values($rAr);
- }
- assoc_unique(&$arr,'name');
- print_r($arr);
- ?>
利用php自带函数,说明
array array_unique ( array $array )
array_unique() 接受 array 作为输入并返回没有重复值的新数组。
注意键名保留不变。array_unique() 先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。
Note: 当且仅当 (string) $elem1 === (string) $elem2 时两个单元被认为相同。就是说,当字符串的表达一样时。第一个单元将被保留。
Example #1 array_unique() 例子
- <?php
- $input = array("a" => "green", "red", "b" => "green", "blue", "red");
- $result = array_unique($input);
- print_r($result);
- ?>
上例将输出:
Example #2 array_unique() 和类型
- <?php
- $input = array(4, "4", "3", 4, 3, "3");
- $result = array_unique($input);
- var_dump($result);
- ?>
上例将输出:
波比源码 » 删除数组与二维数组值的php代码
levaquin 250mg canada levaquin 250mg cheap
avodart for sale online buy dutasteride without prescription purchase ondansetron online
fildena without prescription sildenafil 50mg for sale robaxin 500mg generic
order suhagra 100mg pill order sildenafil 50mg generic estrace 1mg brand
lamictal 200mg drug order lamictal 50mg pill cost retin cream
tadalis 20mg canada tadalafil online diclofenac 100mg sale
accutane 40mg usa amoxicillin 1000mg oral buy azithromycin online
arimidex 1mg drug buy anastrozole sale cheap viagra
cialis 5mg en ligne cialis 5mg gГ©nГ©rique acheter 50mg du viagra
purchase prednisone for sale purchase sildenafil generic buy viagra 50mg without prescription
accutane 40mg brand oral azithromycin ivermectin 15 mg
order buspirone 5mg online cheap oxybutynin 2.5mg generic order generic oxybutynin
purchase terazosin for sale actos tablet buy azulfidine generic
buy benicar 10mg online buy benicar 10mg pills diamox 250mg pill
imdur buy online buy generic azathioprine telmisartan 80mg sale
purchase bupropion without prescription strattera 25mg generic quetiapine usa
order imuran generic sildenafil pill order viagra online cheap
naltrexone canada letrozole medication abilify 20mg canada
order medroxyprogesterone 10mg without prescription buy medroxyprogesterone 10mg pills order periactin
accutane 10mg brand purchase amoxil without prescription order prednisone 40mg generic
azithromycin order order azithromycin pill cheap gabapentin 800mg
order lasix 40mg doxycycline 200mg ca hydroxychloroquine for sale
tadalafil canada oral tadalafil 20mg clomipramine for sale
order zyprexa 10mg pills order olanzapine 10mg generic purchase valsartan
norvasc 5mg uk cialis tadalafil 10mg buy tadalafil 20mg online
sildenafil 100mg pill viagra 50 mg order lisinopril without prescription
buy omeprazole 10mg generic prilosec 20mg cost online blackjack free
generic zyvox 600mg spins real money online gambling games
order vardenafil buy pregabalin for sale methylprednisolone 16mg oral
essay helper online usa viagra sales order sildenafil online
cialis super active tadalafil for sale online viagra 25mg for sale
aristocort 10mg sale buy desloratadine online cheap order clarinex 5mg sale
cialis price costco order cialis without prescription sildenafil for sale
oral xenical 120mg acyclovir 400mg oral purchase zovirax generic
order tadalafil 20mg online cheap otc cialis order clopidogrel 150mg pill
methotrexate drug order warfarin 5mg pills purchase reglan
lioresal for sale online buy toradol 10mg online toradol pills
order imitrex online cheap imitrex 25mg uk order avodart 0.5mg generic
order ranitidine 150mg pills celebrex 200mg pills celebrex order online
best online casino usa best online poker sites for real money casino game
purchase fluconazole order diflucan 100mg online cheap buy sildenafil 100mg online cheap
world poker online free slot games online tadalafil 10mg ca
custom written papers website to write essays stromectol over the counter
casino play ed pills that really work cialis 20mg over the counter
retin sale order tretinoin gel online order avana 100mg
terbinafine 250mg over the counter terbinafine 250mg for sale amoxicillin pill
clarithromycin 250mg drug catapres 0.1 mg canada buy meclizine for sale
buy naproxen pill buy omnicef 300 mg online cheap prevacid 30mg cheap
spiriva over the counter oral terazosin 5mg order terazosin 1mg generic
buy pioglitazone usa viagra sales order viagra pills
buy montelukast 10mg order sildenafil 100mg without prescription us viagra
buy generic nifedipine order nifedipine 10mg generic fexofenadine pills
cheap essay online leflunomide 20mg cost order azulfidine 500mg online cheap
asacol pill mesalamine 800mg tablet purchase avapro online cheap
order diamox 250 mg generic buy imdur 20mg without prescription imuran 50mg brand
buy clobetasol buy cordarone for sale buy cordarone 200mg online
buy coreg 6.25mg sale order oxybutynin 2.5mg online cheap generic amitriptyline
amoxicillin 1000mg oral order stromectol 12mg generic ivermectin 2%
Thank you for great information. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
famotidine generic order famotidine order remeron 15mg pill
Thank you for content. Area rugs and online home decor store. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
buy tricor online sildenafil overnight sildenafil 50mg cost
order tadalafil for sale cost amoxicillin buy amoxicillin online cheap
Thank you for great article. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
tadalafil max dose Best way to take cialis viagra 100mg for sale
tadalafil us top ed drugs where to buy ed pills online
Great post thank you. Hello Administ . Onwin , Onwin Giriş , Onwin Güncel Giriş , Onwin Yeni Adres , onwin
purchase modafinil sale brand promethazine 25mg promethazine uk
buy deltasone 20mg without prescription brand deltasone 20mg buy amoxil 250mg generic
fildena 100mg brand buy generic proscar 1mg order propecia generic
Nice article inspiring thanks. Hello Administ . Websitemiz : https://www.istanbulofistasima.net/
purchase ventolin ventolin for sale augmentin pills
buy provigil 200mg generic oral modafinil 100mg buy metoprolol 100mg online cheap
cheap prednisolone for sale oral prednisolone order furosemide 100mg for sale
avodart for sale online xenical over the counter buy generic orlistat
buy imuran order micardis 80mg online purchase naproxen
dapsone 100mg cheap oral tenormin 50mg order atenolol 50mg for sale
sildenafil mail order viagra mail order tadalafil 10mg for sale