Write a php a function to remove a specified duplicate entry from an array

Last update on August 19 2022 21:50:29 (UTC/GMT +8 hours)

PHP Array: Exercise-44 with Solution

Write a PHP a function to remove a specified duplicate entry from an array.

Sample Solution:

PHP Code:

 $array_value) 
    { 
        if ( ($count > 0) && ($array_value == $value) ) 
        { 
            unset($my_array[$array_key]); 
        } 
        
        if ($array_value == $value) $count++; 
    } 
    
    return array_filter($my_array); 
} 
$numbers = array(4, 5, 6, 7, 4, 7, 8);

print_r(array_uniq($numbers, 7));
?>

Sample Output:

Array                                                       
(                                                           
    [0] => 4                                                
    [1] => 5                                                
    [2] => 6                                                
    [3] => 7                                                
    [4] => 4                                                
    [6] => 8                                                
)     

Flowchart:

Write a php a function to remove a specified duplicate entry from an array

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP script to merge two commas separated lists with unique value only.
Next: Write a PHP script to do a multi-dimensional difference, i.e. returns values of the first array that are not in the second array.

PHP: Tips of the Day

PHP: How to pass an array within a query string?

Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.

Three possible ways to send multi-value fields or arrays would be:

  • ?cars[]=Saab&cars[]=Audi (Best way- PHP reads this into an array)
  • ?cars=Saab&cars=Audi (Bad way- PHP will only register last value)
  • ?cars=Saab,Audi (Haven't tried this)

Form Examples

On a form, multi-valued fields could take the form of a select box set to multiple:

(NOTE: In this case, it would be important to name the select control some_name[], so that the resulting request vars would be registered as an array by PHP)

or as multiple hidden fields with the same name:




NOTE: Using field[] for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.

UPDATE

As commenters have pointed out, this is very much framework-specific. Some examples:

Query string:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3 

Rails:

"list_a": "3", 
"list_b":[
    "1",
    "2",
    "3"
  ], 
"list_c": "1,2,3"

Angular:

"list_a": [
    "1",
    "2",
    "3"
  ],
  "list_b[]": [
    "1",
    "2",
    "3"
  ],
  "list_c": "1,2,3"

(Angular discussion)

Maintaining order: One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.

Ref : https://bit.ly/2FGfPLj

This example is just an alternative.

    

How do you remove duplicates from an array in PHP?

The array_unique() function removes 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.

Which function remove duplicate values from an array?

Use the filter() method: The filter() method creates a new array of elements that pass the condition we provide. It will include only those elements for which true is returned. We can remove duplicate values from the array by simply adjusting our condition.

How are duplicates removed from an array without using any library PHP?

Remove duplicate elements from array in PHP Using array_unique. The array_unique() function removes duplicate elements and values from an array.

How can I remove duplicate values from a multi dimensional array in PHP?

array_unique in PHP The array_unique() function are used to remove duplicate data from given array. There are two parameters that will be passed in the array_unique() , first specifying an array that is required and the second is optional specifying the sorting type.