【问题标题】:removing duplicates from an array in php从php中的数组中删除重复项
【发布时间】:2012-04-25 12:24:39
【问题描述】:

我想从 php.ini 中的数组中删除重复元素。以下是数组的结构

Array
(
    [0] => Array
        (
            [0] => xllga@hotmail.com
            [1] => bounce@abc.com
            [2] => 20120416135504.21734.qmail@abc.com
            [3] => xllga@hotmail.com
            [4] => info@abc.com
            [5] => info@abc.com
            [6] => xllga@hotmail.com
            [7] => xllga@hotmail.com
        )

)

怎么办?

【问题讨论】:

  • 这些数据是使用 SQL 从表中拉回的吗?如果是这样,问题不能在那里解决吗? (a la GROUP BY) 只收到独特的回复?
  • 尝试在php中使用array_unique

标签: php arrays duplicates array-unique


【解决方案1】:

试试array_unique

代码:

<?php

$arr = array_unique($arr);

?>

数组 array_unique ( 数组 $array [, int $sort_flags = SORT_STRING ])

接受一个输入数组并返回一个没有重复值的新数组。

【讨论】:

【解决方案2】:

试试array_unique():

$newArray = array_unique($oldArray);

来自文档:

[array_unique()] 接受一个输入数组并返回一个没有重复值的新数组。

【讨论】:

    【解决方案3】:

    http://php.net/manual/en/function.array-unique.php

    $new_unique_array = array_unique($your_array[0]);
    

    希望对您有所帮助, 斯蒂芬

    【讨论】:

      【解决方案4】:

      没有 array_unique() 解决方案。没那么聪明:)

      array_keys(array_flip($array));
      

      如果使用你的数组,$array = $yourArray[0];

      【讨论】:

        猜你喜欢
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-22
        • 2011-06-29
        • 2020-03-20
        相关资源
        最近更新 更多