删除PHP数组中的重复元素的实现代码
作者:php中文网 时间:2023-06-06 21:19:46
本文我们讲述通过 array_unique()函数删除数组中重复元素。
array_unique()
函数,将数组元素的值作为字符串排序,然后对每个值只保留第一个键名,忽略所有后面的键名,就是删除数组中重复的元素,
语法格式如下:
array arry_unique(array array)
参数 array 为输入的数组。
下面实例使 array_unique()函数删除数组中重复的元素,具体示例代码如下:
<?php
header("Content-Type:text/html; charset=utf-8");
$array_push = array("PHP中文网","www.php.cn", "www.php.cn", "PHP 从入门到精通");//定义数组
array_push($array_push,"百度一下","www.php.cn");//添加元素
print_r($array_push);
echo "<br>";
$result=array_unique($array_push);//删除数组中重复的元素
print_r($result); //输出删除后的数组
?>
输出结果为:
array_unique()
函数只适用于一维数组,对多维数组并不适用,不过你可以在二维数组中的值,用array_unique()即可。
下面实例就是使用array_unique()函数实现删除二维数组的重复元素,具体代码如下:、
<?php
$array = array(array(1,1,2),array(2,3,4,3));
print_r($array);
$temp_array = array();
foreach($array as $key=>$value){
$temp_array[$key] = array_unique($value);
}
$array = $temp_array;
echo "<br>";
print_r($array);
?>
输出结果为:
标签:PHP数组,删除,重复元素
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3.9最新版下载与安装图文教程详解(Windows系统为例)
2023-12-13 20:26:51
![](https://img.aspxhome.com/file/2023/8/115448_0s.png)
python实现126邮箱发送邮件
2022-07-29 23:37:56
![](https://img.aspxhome.com/file/2023/9/91969_0s.jpg)
python基于搜索引擎实现文章查重功能
2022-01-21 19:25:50
![](https://img.aspxhome.com/file/2023/2/67322_0s.png)
Python Dict找出value大于某值或key大于某值的所有项方式
2023-10-16 19:54:55
Python利用3D引擎制作一个3D迷宫游戏
2021-02-18 21:17:54
![](https://img.aspxhome.com/file/2023/8/75368_0s.jpg)
asp如何对多个条件进行判断?
2009-11-20 18:28:00
python 实现围棋游戏(纯tkinter gui)
2023-11-23 16:13:04
![](https://img.aspxhome.com/file/2023/4/92074_0s.gif)
ASP保存远程图片到本地 同时取得第一张图片并创建缩略图的代码
2011-04-19 11:07:00
Python实现最大子序和的方法示例
2023-04-08 03:30:38
![](https://img.aspxhome.com/file/2023/6/131686_0s.png)
Python基于pygame实现图片代替鼠标移动效果
2023-10-24 13:33:34
![](https://img.aspxhome.com/file/2023/9/109329_0s.png)
JavaScript中诡异的delete操作符
2024-04-10 16:15:33
Python的三个重要函数详解
2022-05-19 04:58:45
![](https://img.aspxhome.com/file/2023/9/100859_0s.png)
Python实现判断一个整数是否为回文数算法示例
2022-05-13 12:01:07
![](https://img.aspxhome.com/file/2023/1/94181_0s.png)
Python字典查找数据的5个基础操作方法
2021-09-08 23:19:45
在Python中通过getattr获取对象引用的方法
2023-08-24 23:32:33
phpMyAdmin 2.10.2 配置方法
2007-08-22 08:11:00
![](https://img.aspxhome.com/file/UploadPic/20078/22/200782281529614s.jpg)
从零开始做一个pagination分页组件
2024-04-29 13:25:13
![](https://img.aspxhome.com/file/2023/7/132667_0s.jpg)
详解运行Python的神器Jupyter Notebook
2022-01-20 13:21:56
![](https://img.aspxhome.com/file/2023/3/124783_0s.jpg)
BP神经网络原理及Python实现代码
2022-09-04 21:12:24
![](https://img.aspxhome.com/file/2023/8/72148_0s.jpg)
mysql与sqlserver的所有区别
2009-02-27 16:18:00