How can i get only duplicate values from an array in php?
You will need to make your function case insensitive to get the "Hello" => "hello" result you are looking for, try this method: Show
Output is:
Edit by @AlixAxel: This answer is very misleading. It only works in this specific condition. This counter-example:
Fails miserably. Also, this is not the way to keep duplicates:
Since one of the duplicated values will be in Edit by @RyanDay: So look at @Srikanth's or @Bucabay's answer, which work for all cases (look for case insensitive in Bucabay's), not just the test data specified in the question.
10 Years Ago Show only duplicate values from array without built in function $arr = array(3,5,2,5,3,9); Edited 10 Years Ago by rajendra87 Recommended Answers
Jump to Post
Jump to Post All 7 Replies
10 Years Ago
The logic here is simple: I am not a php developer so i am using c++ to explain this method ! Try to make the code for php yourself !
10 Years Ago
I saw this same question
here ! if you did not post it then here is the code:
10 Years Ago that question was posted by me @rainbowMatrix
blocblue 238 Posting Pro in Training Featured Poster 10 Years AgoHow about something outside of the box:
phorce 131 Posting Whiz in Training Featured Poster 10 Years AgoFinally got there:
I doubt it will work for words, or characters, for this you will have to manipulate the array_keys. Output: 3, 5, Edited 10 Years Ago by phorce
renuv 0 Newbie Poster 6 Years Ago
diafol 6 Years AgoThanks for necroposting this dead thread back to the current list - not. Reply to this topic Be a part of the DaniWeb community We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge. How can I get repeat value from an array in PHP?Return Value: The array_unique() function returns the filtered array after removing all duplicates from the array. print_r( array_unique ( $a ));
How do you find duplicate values in an array?function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = 0; j < myArray. length; j++) { if (i != j) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } } return false; // means there are no duplicate values. }
How do you find the duplicate number on a given integer array in PHP?This is the only answer that returns only the duplicate values. – Redzarf. ... . Extended to only give list of duplicate values (what I needed): return array_values(array_unique(array_diff_key($a, array_unique($a)))); – Autumn Leonard. ... . Stop scrolling. Although it doesn't seem, this is the answer you are looking for.. How can I get unique values from two arrays in PHP?The array_diff() (manual) function can be used to find the difference between two arrays: $array1 = array(10, 20, 40, 80); $array2 = array(10, 20, 100, 200); $diff = array_diff($array1, $array2); // $diff = array(40, 80, 100, 200);
|