1

I'm trying to delete duplicate arrays.. but it seems not to work...

this is my array:

array
(
[1] => Array
    (
        [id] => 141
        [yt] => 5
        [PHD] => Jan
        [type] => Mercedes
        [aid] => 5
    )

[2] => Array
    (
        [id] => 2
        [yt] => 5
        [PHD] => Jan
        [type] => Mercedes
        [aid] => 5
    )

[3] => Array
    (
        [id] => 3
        [yt] => 5
        [PHD] => Jan
        [type] => Mercedes
        [aid] => 5
    )

[4] => Array
    (
        [id] => 10
        [yt] => 5
        [PHD] => Jan
        [type] => Mercedes
        [aid] => 5
    )

[5] => Array
    (
        [id] => 4
        [yt] => 5
        [PHD] => Peter
        [type] => Audi
        [aid] => 5
    )
)

OUTPUT

[5] => Array
    (
        [id] => 4
        [yt] => 5
        [PHD] => Peter
        [type] => Audi
        [aid] => 5
    )

The array creates with a SQL query so I foreach the array:

  foreach($All_cars as $key=>$row) {
  .....
  print_r($All_cars);
  }

So that outputs the whole array.

I tried this but no effect.

 $result = array_unique($All_cars );

I hope you guys can help me... cause I tried this 1 whole day already.

Thanks for your help.

Wolverine
  • 1,712
  • 1
  • 15
  • 18
David Brett
  • 59
  • 1
  • 8
  • 7
    Why don't you put DISTINCT in your query? – Mayank Pandeyz Dec 15 '16 at 10:30
  • 1
    From the DOCS: `Note: Note that array_unique() is not intended to work on multi dimensional arrays.` – Jeff Dec 15 '16 at 10:32
  • @MayankPandeyz cause I use the query for serval things. – David Brett Dec 15 '16 at 10:49
  • @Jeff Are there another solutions or manipulates to use unique? – David Brett Dec 15 '16 at 10:50
  • check @http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php this link may help you – ayush Dec 15 '16 at 10:56
  • @ayush thanks for the link.. The array_unique works only if everthing of the array is the same. I just need to recognize when one key is the same. – David Brett Dec 15 '16 at 12:05
  • The link provided by @ayush is the exact solution you might be looking for. I also had an assumption that if you need to remove all the data with duplication leaving only the data that's not duplicated. Right? – Wolverine Dec 15 '16 at 12:09
  • @Perumal93 I forgot to implement the id. So every array has a identifier ID. The array_unique removes only if the arrays are exaclty the same.. I have to find out a another solution that just recognize the key [type] or [PHD] . But thanks. – David Brett Dec 15 '16 at 12:12
  • Ah. This is what I was thinking why ID of each array had same. – Wolverine Dec 15 '16 at 12:13
  • Sorry my bad :p – David Brett Dec 15 '16 at 12:17

2 Answers2

2

Try This

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

Or

$array = [
    [
        'id' => '123',
        'foo' => 'aaa',
        'bar' => 'bbb'
    ],
    [
        'id' => '123',
        'foo' => 'ccc',
        'bar' => 'ddd'
    ],
    [
        'id' => '567',
        'foo' => 'eee',
        'bar' => 'fff'
    ]
];

$ids = array_column($array, 'id');
$ids = array_unique($ids);
$array = array_filter($array, function ($key) use ($ids) {
    return in_array($key, array_keys($ids));
}, ARRAY_FILTER_USE_KEY);

The result is:

Array
(
    [0] => Array
        (
            [id] => 123
            [foo] => aaa
            [bar] => bbb
        )

    [2] => Array
        (
            [id] => 567
            [foo] => eee
            [bar] => fff
        )

)

Or

if you need to eliminate duplicates on specific keys, such as a mysqli id, here's a simple funciton

function search_array_compact($data,$key){
    $compact = [];
    foreach($data as $row){
        if(!in_array($row[$key],$compact)){
            $compact[] = $row;
        }
    }
    return $compact;
}

Note You can pass an array of keys and add an outer foreach, but it will be 2x slower per additional key.

Or
You can use an associative array.

$temp_array = array();
foreach ($array as &$v) {
    if (!isset($temp_array[$v['name']]))
        $temp_array[$v['name']] =& $v;
}

This creates a temporary array, using $v['name'] as the key. If there is already an element with the same key, it is not added to the temporary array.

You can convert the associative array back to a sequential array, using

$array = array_values($temp_array);

Example code and output: http://codepad.org/zHfbtUrl

Wolverine
  • 1,712
  • 1
  • 15
  • 18
Ketan Borada
  • 856
  • 9
  • 24
  • The second one :p with the array_filter – David Brett Dec 15 '16 at 12:51
  • 1
    You need to correct the second method. Swap the variables in the parameter of callback function passed in `array_filter` to avoid misconception. The order of parameter names should be like `function ($value, $key) use ($ids) {` and also `return in_array($key, array_keys($ids));`. Also, you don't need to use `ARRAY_FILTER_USE_BOTH` if you're only going to use key of the array and `ARRAY_FILTER_USE_KEY` is enough and remove the `$value` parameter leaving only `$key` parameter. – Wolverine Dec 15 '16 at 14:59
  • @Perumal93 Thanks, I changed it. :) – David Brett Dec 15 '16 at 15:53
  • @Perumal93 you can edit answer if sure,i've not tested. – Ketan Borada Dec 16 '16 at 05:40
  • The FILTER_USE_KEY takes less memory so It's way faster. – David Brett Dec 16 '16 at 08:47
  • 1
    Using `ARRAY_FILTER_USE_BOTH` when only key of the elements are used rather than values is a kind of quite more memory consumption and using `ARRAY_FILTER_USE_KEY` is alone enough only if you're going to work with keys because this avoids redundant code and also prevents from memory consumption keeping performance as much simpler as it could. – Wolverine Dec 16 '16 at 11:54
0

try this

for($i=0;  $i < count($array); $i++)
{
    for($j=0;  $j < count($array); $j++)
    {
        if($array[$i] == $array[$j])
        {
            unset $array[$i];
        }
    }
}
hrishi
  • 1,610
  • 6
  • 26
  • 43