1

I want to remove the values from array which are same. For example: This is the array.

Array ( [0] => 1 [1] => 63 [2] => 1 )

is there any function or something in core php which return me only the value which is not duplicate i.e value with index number 1 and delete index 0 and 2, I want the result

Array ( [1] => 63) 

Is there any way?

  • $array = array_values($array); – Koen Hollander Apr 16 '18 at 12:24
  • 1
    @KoenHollander - How would that function help the OP? In this case, it should give you an identical array again. – M. Eriksson Apr 16 '18 at 12:26
  • Apologies i didn't realise you wanted to remove all instances of a duplicate I have found another question that looks like its a similar question as yours: https://stackoverflow.com/questions/3691369/how-can-i-remove-all-duplicates-from-an-array-in-php – ArcherBoy27 Apr 16 '18 at 12:27

4 Answers4

4

You can use array_filter() and array_count_values() to check the count is not greater then 1.

<?php
$data = [1, 63, 1];

$data = array_filter($data, function ($value) use ($data) {
    return !(array_count_values($data)[$value] > 1);
});

print_r($data);

https://3v4l.org/uIVLN

Result:

Array
(
    [1] => 63
)

Will also work fine with multiple dupes: https://3v4l.org/eJSTY

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
1

One option is to use array_count_values to count the values. Loop and push only the 1 values

$arr = array(1,63,1);
$arrKey = array_flip( $arr ); //Store the key

$result = array();  
foreach( array_count_values($arr) as $k => $v ) {
    if ( $v === 1 ) $result[ $arrKey[$k] ] = $k;
}

echo "<pre>";
print_r( $result );
echo "</pre>";

This will result to:

Array
(
    [1] => 63
)

Doc: array_count_values

Eddie
  • 26,593
  • 6
  • 36
  • 58
  • The values are dynamic. Now how should i assign the key to be a value in another array. Because i have to store that unique array back to the database – Mohammad Faizan Zafar Apr 16 '18 at 12:36
  • @MohammadFaizanZafar - I stored the key on $arrKey. It is okay to use `array_flip` because you only need the unique values. – Eddie Apr 16 '18 at 12:41
  • 1
    I reckon this answer is the most intelligent of the lot. Always avoid performing iterated function calls when possible, this makes the approach very scalable. When the input becomes very large, it is instantly compacted with the one-time call of `array_count_values()`. Caching the flipped input is also very wise. My only concern -and it's not a big one- is that I have read that placing a function as the input of a foreach loop impacts performance. I don't know if that is true in this case, and I know it improves code brevity, but perhaps it is better declared prior to the loop. Great answer. – mickmackusa Apr 27 '18 at 13:53
1

I believe that the best solution is the following:

function removeDuplicates(array $initialArray) : array 
{
    // Remove duplicate values from an array
    $uniqueValues = array_unique($initialArray);

    // Computes the difference of arrays with additional index check
    $duplicateValues = array_diff_assoc($initialArray, $uniqueValues);

    // Removed any values in both arrays
    return array_diff($uniqueValues, $duplicateValues);
}

This solution utilises the following functions in PHP:

array_unique

array_diff_asoc

array_diff

B3none
  • 385
  • 3
  • 18
0

You can use array_keys() with a search value as the second parameter to see how many times a value exists.

$array = [1, 63, 1, 12, 64, 12];

$new = [];
foreach ($array as $value) {
    // Get all keys that have this value. If there's only one, save it.
    if (count(array_keys($array, $value)) == 1) {
        $new[] = $value;
    }
}

Demo: https://3v4l.org/G1DCg

I don't know the performance of this compared to the other answers. I leave the profiling to someone else.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40