Remove duplicated values completely deleted from array in php

php remove duplicates from multidimensional array
php remove duplicates from multidimensional array by key
remove duplicate elements from array in php without using function
php remove duplicates from array of objects
remove duplicate value from array in php
find duplicate values in array php
php remove duplicates from associative array
php find duplicate values in associative array

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?

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

Java Program to Remove Duplicate Element in an Array, . Parameters: This function accepts two parameters out of which one is mandatory and the other is optional. Answer: Use the PHP array_unique() function You can use the PHP array_unique() function to remove the duplicate elements or values form an array. If the array contains the string keys, then this function will keep the first key encountered for every value, and ignore all the subsequent keys.

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

PHP, Sorts an associative array in descending order, according to the key Remove Duplicate Values From An Array In PHP If two or more array values are the same, the first appearance will be kept, and the other will be removed. The returned array will hold the first array item’s key type. The syntax of array_unique() function is following.

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

PHP Array Functions, Sorts an associative array in ascending order, according to the key You can use the PHP array_unique() function remove duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. If two or more array values are the same, the first appearance will be kept and the other will be removed.

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.

PHP array_filter() Function, Here are some more FAQ related to this topic: How to remove empty values from an array in PHP · How to delete an element from an array in PHP · How to check if  PHP array_unique() function: The array_unique() is used to remove duplicate values from an array.

How to Remove Duplicate Values from an Array in PHP, Yo, I'm trying to compare and remove duplicate element (value) from two different arrays. //Call jaz_remove_duplicate_array function rda()  The most powerful solution would be using array_filter, which allows you to define your own filtering function.. But some might say it's a bit overkill, in your situation

Delete duplicate array completely? - PHP, PHP: Removes duplicate values from an array​​ The array_unique() is used to remove duplicate values from an array. Note: The keys are preserved. array_unique() sorts the values treated as a string at first, then will keep the first key encountered for every value, and ignore all following keys. @vivoconunxino I wrote it that way because of the authors question "Best way to clear a PHP array's values". It sais 'clear', not 'new instance'.

PHP : array_unique() function, It takes an input array and returns a new array without duplicate values. Two elements are considered equal if and only if (string) $item1 === (  As for PHP 7.1.12, this is the comparison between array_keys(array_flip()), array_flip(array_flip()), for each elimination and array_unique. The array_keys(array_flip()) is the fastest method to remove duplication values from a single dimension array:

Comments
  • $array = array_values($array);
  • @KoenHollander - How would that function help the OP? In this case, it should give you an identical array again.
  • 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: stackoverflow.com/questions/3691369/…
  • 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
  • @MohammadFaizanZafar - I stored the key on $arrKey. It is okay to use array_flip because you only need the unique values.
  • 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.