专业编程基础技术教程

网站首页 > 基础教程 正文

php怎么用array_unique()函数去除数组中重复的值?

ccvgpt 2025-02-18 10:43:00 基础教程 4 ℃

php去除数组中的重复键值,需要使用array_unique()函数来对数组的键值进行去重。

array_unique() 函数:用于移除数组中重复的值。如果两个或更多个数组值相同,只保留第一个值,其他的值都被移除。

php怎么用array_unique()函数去除数组中重复的值?

注释:被保留的数组将保持第一个数组项的键名类型

输出结果:

array(6) {
  [0]=>
  string(3) "PHP"
  [1]=>
  string(3) "ASP"
  [2]=>
  string(3) "ASP"
  [3]=>
  string(8) "李雷博客"
  [4]=>
  string(3) "PHP"
  [5]=>
  string(7) "PHP博客"
}

删除重复元素后的数组:

array(4) {
  [0]=>
  string(3) "PHP"
  [1]=>
  string(3) "ASP"
  [3]=>
  string(8) "李雷博客"
  [5]=>
  string(7) "PHP博客"
}

Tags:

最近发表
标签列表