0

I have a array and I need unique contents. How can I get rid of duplicates in this $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"
  }

But I want it to look like: (Only unique contents.)

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"
  }
  }

I have tried with:

array_unique($tmparray);

array_unique can't do what I want. Anyone have a idea how to solve this?

Cyborg
  • 1,437
  • 19
  • 40

5 Answers5

2

your question seems duplicate of this
How to remove duplicate values from a multi-dimensional array in PHP

i guess array_map will solve your problem

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Community
  • 1
  • 1
Piyush
  • 74
  • 6
  • Thank you, this really works. But I found an other solution in the Link you provided which is shorter and works as I wanted. – Cyborg Jan 14 '17 at 13:25
0

You can use this code:

$newarray= array();
foreach ($tmparray as $value) {
    if (!in_array($value,$newarray)) {
        $newarray[ ] = $value;
    }
}
var_dump($newarray);
aya
  • 1,597
  • 4
  • 29
  • 59
0

I assume that by duplicate you mean two items where either elements are matching, since you are mapping ads to their pictures, therefore:

$target = array();
$elementCount = count($tmparray);
$newElementCount = 0;
for ($i = 0; $i < $elementCount; $i++) {
    $found = false;
    for ($j = 0; (!$found) && (j < $newElementCount); j++) {
        $found = $target[$j][0] === $tmparray[$i][0] || $target[$j][1] === $tmparray[$i][1];
    }
    if (!$found) {
        $target[$newElementCount++]=$tmparray[$i];
    }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

PHP array_unique is used only for single dimensional arrays, for multidimensional you can do this by serializing multidimensional array, like below

<?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);
?>

This will give you:

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
        )

)

In short you can perform this in single line code with array_map

$dataArray = array_map('unserialize', array_unique(array_map('serialize', $dataArray)));
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
0

I got it working with this line:

$tmparray = array_unique($tmparray, SORT_REGULAR);
Cyborg
  • 1,437
  • 19
  • 40