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.

10
  • 7
    Why don't you put DISTINCT in your query? Commented Dec 15, 2016 at 10:30
  • 1
    From the DOCS: Note: Note that array_unique() is not intended to work on multi dimensional arrays. Commented Dec 15, 2016 at 10:32
  • @MayankPandeyz cause I use the query for serval things. Commented Dec 15, 2016 at 10:49
  • @Jeff Are there another solutions or manipulates to use unique? Commented Dec 15, 2016 at 10:50
  • check @stackoverflow.com/questions/307674/… this link may help you Commented Dec 15, 2016 at 10:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

The second one :p with the array_filter
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.
@Perumal93 Thanks, I changed it. :)
@Perumal93 you can edit answer if sure,i've not tested.
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.
|
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];
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.