PHP:如何从数组中获取唯一内容?

时间:2017-01-14 10:12:57

标签: php arrays

我有一个数组,我需要独特的内容。如何摆脱 $ tmparray

中的重复项
array(176) {
  [0]=>
  array(2) {
    [0]=>
    string(22) "/ads/67006/didi"
    [1]=>
    string(73) "/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"
  }
  [1]=>
  array(2) {
    [0]=>
    string(22) "/ads/67006/didi"
    [1]=>
    string(73) "/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"
  }
  [2]=>
  array(2) {
    [0]=>
    string(22) "/ads/67006/didi"
    [1]=>
    string(73) "/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"
  }
  [3]=>
  array(2) {
    [0]=>
    string(19) "/ads/67010/sylvesta"
    [1]=>
    string(73) "/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"
  }
  [4]=>
  array(2) {
    [0]=>
    string(19) "/ads/67010/sylvesta"
    [1]=>
    string(73) "/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"
  }
  [5]=>
  array(2) {
    [0]=>
    string(19) "/ads/67010/sylvesta"
    [1]=>
    string(73) "/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"
  }

但我希望它看起来像:(只有独特的内容。)

array(176) {
  [0]=>
  array(2) {
    [0]=>
    string(22) "/ads/67006/didi"
    [1]=>
    string(73) "/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"
  }
  [1]=>
  array(2) {
    [0]=>
    string(19) "/ads/67010/sylvesta"
    [1]=>
    string(73) "/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"
  }
  }

我尝试过:

array_unique($tmparray);

array_unique不能做我想要的。任何人都知道如何解决这个问题?

5 个答案:

答案 0 :(得分:2)

你的问题似乎与此相同 How to remove duplicate values from a multi-dimensional array in PHP

我猜array_map会解决你的问题

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

答案 1 :(得分:0)

您可以使用此代码:

/retrace.sh -verbose mapping.txt ~/trace.txt

答案 2 :(得分:0)

我认为复制品是指两个要素匹配的项目,因为您要将广告映射到他们的图片,因此:

trait StrictLogging {
  protected val logger: Logger =
    Logger(LoggerFactory.getLogger(getClass.getName))
}

答案 3 :(得分:0)

PHP array_unique仅用于单维数组,对于多维,您可以通过序列化多维数组来完成此操作,如下所示

<?php 
$dataArray = [
                0=>["/ads/67006/didi","/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"],
                1=>["/ads/67010/sylvesta","/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"],
                2=>["/ads/67006/didi","/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"],
                3=>["/ads/67010/sylvesta","/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"],
             ];
$serilaized = [];
$newArr = [];
### serilaize each node of multi array and create a new single dimention array..         
foreach($dataArray as $val){
    $serilaized[] = serialize($val); 
}
### now perform array unique..
$serilaized_unique = array_unique($serilaized);
## unserialize each node of uniqur array..
foreach($serilaized_unique as $val){
    $newArr[] = unserialize($val);
}
echo "<pre>";print_r($newArr);
?>

这会给你:

Array
(
    [0] => Array
        (
            [0] => /ads/67006/didi
            [1] => /Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg
        )

    [1] => Array
        (
            [0] => /ads/67010/sylvesta
            [1] => /Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg
        )

)

简而言之,您可以使用array_map

在单行代码中执行此操作
$dataArray = array_map('unserialize', array_unique(array_map('serialize', $dataArray)));

答案 4 :(得分:0)

我得到了这条线:

$tmparray = array_unique($tmparray, SORT_REGULAR);