Array_Unique Trong Php

Bạn đang đọc: Array_Unique Trong Php

Array_Unique Trong Php

by Admin _

April 28, 2021

Use array_unique ( ) .Bạn đang xem : Array_unique trong php Example : $array = array(1, 2, 2, 3); $array = array_unique($array); // Array is now (1, 2, 3)
Ian – Note that array_unique() is not intended to work on multi dimensional arrays. – Peter Ajtai Aug 19 “10 at 19:45
The above will preserve elements’ keys. If you want them re-indexed, in addition apply array_values: php.net/manual/en/function.array-values.php – CodeVirtuoso Jan 11 “12 at 13:48
This didnt work for me kindly use: $array = array_unique($array, SORT_REGULAR); – iniravpatel Jan 12 “17 at 18:56
PeterAjtai: Thank You for highlighting in comment section. It saved lot of time for me. – Nana Partykar Dec 12 “18 at 15:08
USD array = array ( 1, 2, 2, 3 ) ; USD array = array_unique ( $ array ) ; / / Array is now ( 1, 2, 3 ) Ian – Note that array_unique ( ) is not intended to work on multi dimensional arrays. – Peter Ajtai Aug 19 ” 10 at 19 : 45T he above will preserve elements ‘ keys. If you want them re-indexed, in addition apply array_values : php.net/manual/en/function.array-values.php – CodeVirtuoso Jan 11 ” 12 at 13 : 48T his didnt work for me kindly use : USD array = array_unique ( $ array, SORT_REGULAR ) ; – iniravpatel Jan 12 ” 17 at 18 : 56P eterAjtai : Thank You for highlighting in comment section. It saved lot of time for me. – Nana Partykar Dec 12 ” 18 at 15 : 08Use array_values ( array_unique ( $ array ) ) ; array_unique : for unique array array_values : for reindexing
+1 array_unique returns an object with key and value pairs AND array_values return only values as an array. – narainsagar Sep 21 “18 at 17:16
//Find duplicates $arr = array( “unique”, “duplicate”, “distinct”, “justone”, “three3”, “duplicate”, “three3”, “three3”, “onlyone” ); $unique = array_unique($arr); $dupes = array_diff_key( $arr, $unique ); // array( 5=>”duplicate”, 6=>”three3″ 7=>”three3″ ) // count duplicates array_count_values($dupes); // array( “duplicate”=>1, “three3″=>2 )
+ 1 array_unique returns an object with key and value pairs AND array_values return only values as an array. – narainsagar Sep 21 ” 18 at 17 : 16 / / Find duplicates USD arr = array ( ” unique “, ” duplicate “, ” distinct “, ” justone “, ” three3 “, ” duplicate “, ” three3 “, ” three3 “, ” onlyone ” ) ; USD unique = array_unique ( $ arr ) ; USD dupes = array_diff_key ( $ arr, $ unique ) ; / / array ( 5 => ” duplicate “, 6 => ” three3 ” 7 => ” three3 ” ) / / count duplicates array_count_values ( $ dupes ) ; / / array ( ” duplicate ” => 1, ” three3 ” => 2 )The only thing which worked for me is : $array = array_unique($array, SORT_REGULAR); USD array = array_unique ( $ array, SORT_REGULAR ) ;Edit : SORT_REGULAR keeps the same order of the original array.

sometimes array_unique() is not the way, if you want get unique AND duplicated items

$unique=array(“”,”A1″,””,”A2″,””,”A1″,””); $duplicated=array(); foreach($unique as $k=>$v) } sort($unique); // optional sort($duplicated); // optional USD unique = array ( ” “, ” A1 “, ” “, ” A2 “, ” “, ” A1 “, ” ” ) ; USD duplicated = array ( ) ; foreach ( USD unique as USD k => $ v ) } sort ( $ unique ) ; / / optional sort ( $ duplicated ) ; / / optionalresults on array ( 0 => “”, 1 => “A1”, 2 => “A2”, ) /* $unique */ array ( 0 => “”, 1 => “”, 2 => “”, 3 => “A1”, ) /* $duplicated */
array ( 0 => ” “, 1 => ” A1 “, 2 => ” A2 “, ) / * USD unique * / array ( 0 => ” “, 1 => ” “, 2 => ” “, 3 => ” A1 “, ) / * USD duplicated * /We can create such type of array to use this last value will be updated into column or key value and we will get unique value from the array … $array = array (1,3,4,2,1,7,4,9,7,5,9); $data=array(); foreach($array as $value ) array_keys($data); OR array_values($data);
USD array = array ( 1,3,4,2,1,7,4,9,7,5,9 ) ; USD data = array ( ) ; foreach ( USD array as $ value ) array_keys ( $ data ) ; OR array_values ( $ data ) ;explode ( “, “, implode ( “, “, array_unique ( explode ( “, “, $ YOUR_ARRAY ) ) ) ) ; This will take care of key associations and serialize the keys for the resulting new array : – )
What would be the effect of explode(“,” $YOUR_ARRAY) when $YOUR_ARRAY is array(“1”, “2”, “3”)? – kiamlaluno Aug 18 “10 at 12:52
kiamlaluno – 🙂 and the answer is: Warning: explode() expects parameter 2 to be string, array given in … on line …. – Jesse Chisholm Jul 13 “15 at 13:57
JesseChisholm Yes, this was the point, but nobody noticed the code, apparently. – kiamlaluno Jul 13 “15 at 16:30
What would be the effect of explode ( “, ” $ YOUR_ARRAY ) when $ YOUR_ARRAY is array ( ” 1 “, ” 2 “, ” 3 ” ) ? – kiamlaluno Aug 18 ” 10 at 12 : 52 kiamlaluno – 🙂 and the answer is : – Jesse Chisholm Jul 13 ” 15 at 13 : 57J esseChisholm Yes, this was the point, but nobody noticed the code, apparently. – kiamlaluno Jul 13 ” 15 at 16 : 30Depending on the size of your array, I have found $array = array_values( array_flip( array_flip( $array ) ) ); USD array = array_values ( array_flip ( array_flip ( $ array ) ) ) ;can be faster than array_unique.
Any more information on what’s going on here and would it be faster with a bigger or smaller array. – Fi Horan Aug 31 “16 at 10:49
The double flip is going to remove duplicated values, because a key can’t exist twice, otherwise it gets overwritten. If any value is duplicated and the array is flipped, the last occurrence (I assume) will be the value for the key. – Goldentoa11 Jan 18 “17 at 13:42
In PHP 7 I’ve noticed flipping a multidimensional array more than once may reorder array elements unexpectedly. – Josh Habdas May 31 “17 at 2:32
$a = array(1, 2, 3, 4); $b = array(1, 6, 5, 2, 9); $c = array_merge($a, $b); $unique = array_keys(array_flip($c)); print_r($unique);
The quickest way to achieve this is to use the array_flip function built-in to PHP<1>. array_flip will swap the array values with their keys and since an array cannot have duplicate keys you will end up with a unique set of keys that correspond to the values of the original array. To retrieve these keys as values you can use the array_keys function to retrieve your unique values. Both array_flip and array_keys are worst-case O(n) functions while array_unique has a worst-case of O(n log(n)).<2> – pawan kumar Apr 10 “19 at 12:39
1
Please add some more explanation to your answer (not to the comment section!). How does the given code remove duplicate values from a single array? Why do you need two arrays for that? – Nico Haase Apr 10 “19 at 13:05
Welcome to darkedeneurope.com! I see that you have added some explanation in the comments of your answer, it would be helpful if you add this information as part of your answer itself. – n4m31ess_c0d3r Apr 10 “19 at 13:08
Seems more reasonable to add that comment as an edit to already long-existing answer (darkedeneurope.com/a/52591730/2109067). – ankhzet Apr 10 “19 at 13:38
You can use single array with duplicate elements. I had problem of getting values from two arrays into one then remove duplicates. – pawan kumar Apr 11 “19 at 15:21
Add a comment  | 
1
Any more information on what’s going on here and would it be faster with a bigger or smaller array. – Fi Horan Aug 31 ” 16 at 10 : 49T he double flip is going to remove duplicated values, because a key can’t exist twice, otherwise it gets overwritten. If any value is duplicated and the array is flipped, the last occurrence ( I assume ) will be the value for the key. – Goldentoa11 Jan 18 ” 17 at 13 : 42I n PHP 7 I’ve noticed flipping a multidimensional array more than once may reorder array elements unexpectedly. – Josh Habdas May 31 ” 17 at 2 : 32 $ a = array ( 1, 2, 3, 4 ) ; USD b = array ( 1, 6, 5, 2, 9 ) ; USD c = array_merge ( $ a, $ b ) ; USD unique = array_keys ( array_flip ( $ c ) ) ; print_r ( $ unique ) ; The quickest way to achieve this is to use the array_flip function built-in to PHP < 1 >. array_flip will swap the array values with their keys and since an array cannot have duplicate keys you will end up with a unique set of keys that correspond to the values of the original array. To retrieve these keys as values you can use the array_keys function to retrieve your unique values. Both array_flip and array_keys are worst-case O ( n ) functions while array_unique has a worst-case of O ( n log ( n ) ). < 2 > – pawan kumar Apr 10 ” 19 at 12 : 39P lease add some more explanation to your answer ( not to the comment section ! ). How does the given code remove duplicate values from aarray ? Why do you need two arrays for that ? – Nico Haase Apr 10 ” 19 at 13 : 05W elcome to darkedeneurope.com ! I see that you have added some explanation in the comments of your answer, it would be helpful if you add this information as part of your answer itself. – n4m31ess_c0d3r Apr 10 ” 19 at 13 : 08S eems more reasonable to add that comment as an edit to already long-existing answer ( darkedeneurope.com/a/52591730/2109067 ). – ankhzet Apr 10 ” 19 at 13 : 38Y ou can use single array with duplicate elements. I had problem of getting values from two arrays into one then remove duplicates. – pawan kumar Apr 11 ” 19 at 15 : 21A dd a comment |That ” s a great way to do it. Might want to make sure its output is back an array again. Now you ” re only showing the last unique value. Try this : $arrDuplicate = array (“”,””,1,3,””,5); foreach (array_unique($arrDuplicate) as $v) } print_r ($arrRemoved);
Share
Improve this answer
Follow
edited Oct 5 “12 at 8:56
Edson Medina
8,302 3 3 gold badges 37 37 silver badges 48 48 bronze badges
answered Aug 9 “12 at 9:53
Dries BDries B
21 3 3 bronze badges
Add a comment  | 
1
if (
!in_array($classified->category,$arr)) endwhile; wp_reset_query(); ?> USD arrDuplicate = array ( ” “, ” “, 1,3, ” “, 5 ) ; foreach ( array_unique ( $ arrDuplicate ) as USD v ) } print_r ( $ arrRemoved ) ; ShareImprove this answerFollowedited Oct 5 ” 12 at 8 : 56E dson Medina8, 302 3 3 gold badges 37 37 silver badges 48 48 bronze badgesanswered Aug 9 ” 12 at 9 : 53D ries BDries B21 3 3 bronze badgesAdd a comment | if ( ! in_array ( USD classified -> category, $ arr ) ) endwhile ; wp_reset_query ( ) ; ? >first time check value in array and found same value ignore it
Share
Improve this answer
Follow
edited Jun 15 “17 at 5:51
answered Jun 15 “17 at 5:42
Alpesh NavadiyaAlpesh Navadiya
25 5 5 bronze badges
Add a comment  | 
1
ShareImprove this answerFollowedited Jun 15 ” 17 at 5 : 51 answered Jun 15 ” 17 at 5 : 42A lpesh NavadiyaAlpesh Navadiya25 5 5 bronze badgesAdd a comment |Remove duplicate values from an associative array in PHP. $arrDup = Array (“0” => “aaa-aaa”, “SKU” => “aaa-aaa”, “1” => “12/1/1”, “date” => “12/1/1”, “2” => “1.15”, “cost” => “1.15” ); foreach($arrDup as $k => $v) USD arrDup = Array ( ” 0 ” => ” aaa-aaa “, ” SKU ” => ” aaa-aaa “, ” 1 ” => ” 12/1/1 “, ” date ” => ” 12/1/1 “, ” 2 ” => ” 1.15 “, ” cost ” => ” 1.15 ” ) ; foreach ( USD arrDup as USD k => $ v )Array ( < 0 > => aaa-aaa < 1 > => 12/1/1 < 2 > => 1.15 )
Share
Improve this answer
Follow
answered Feb 12 “18 at 20:39
ShivivanandShivivanand
11 2 2 bronze badges
Add a comment  | 
1
ShareImprove this answerFollowanswered Feb 12 ” 18 at 20 : 39S hivivanandShivivanand11 2 2 bronze badgesAdd a comment |There can be multiple ways to do these, which are as follows //first method $filter = array_map(“unserialize”, array_unique(array_map(“serialize”, $arr))); //second method $array = array_unique($arr, SORT_REGULAR);
Share
Improve this answer
Follow
answered Jun 5 “18 at 8:33
Shahrukh AnwarShahrukh Anwar
1,884 1 1 gold badge 18 18 silver badges 18 18 bronze badges
Add a comment  | 
1
/ / first method USD filter = array_map ( ” unserialize “, array_unique ( array_map ( ” serialize “, $ arr ) ) ) ; / / second method USD array = array_unique ( $ arr, SORT_REGULAR ) ; ShareImprove this answerFollowanswered Jun 5 ” 18 at 8 : 33S hahrukh AnwarShahrukh Anwar1, 884 1 1 gold badge 18 18 silver badges 18 18 bronze badgesAdd a comment |If you concern in performance and have simple array, use : array_keys(array_flip($array)); array_keys ( array_flip ( $ array ) ) ;It ” s many times faster than array_unique .Xem thêm : Top 3 Cách Tải trò chơi Qua Thẻ Nhớ 2020, Chuyển Ứng Dụng Sang Thẻ Nhớ
Share
Improve this answer
Follow
answered Oct 1 “18 at 12:59
michal.jakubeczymichal.jakubeczy
4,185 1 1 gold badge 29 29 silver badges 41 41 bronze badges
Add a comment  | 
0
$arrDuplicate = array (“”,””,1,3,””,5); foreach(array_unique($arrDuplicate) as $v)} print_r($arrRemoved);
Share
Improve this answer
Follow
edited Nov 14 “11 at 9:15
Verbeia
4,370 2 2 gold badges 19 19 silver badges 44 44 bronze badges
answered Nov 14 “11 at 9:09
user1045247user1045247
11
2
1
This really does not explain anything, and it does not seem to be “smarter”. – Sven Nov 6 “12 at 23:24
You can always explain why by editing your own post, just click on the “edit” link at the end of your answer. I also suggest that you have a look to the FAQ : darkedeneurope.com/faq – ForceMagic Nov 6 “12 at 23:24
Add a comment  | 
0
function arrayUnique($myArray) else if (!empty($val2)) } } return ($newArray); } function deleteEmpty($myArray) } return $retArray; }
Share
Improve this answer
Follow
edited Nov 6 “12 at 23:30
Chris Forrence
9,633 11 11 gold badges 43 43 silver badges 59 59 bronze badges
answered Nov 5 “12 at 11:11
Vineesh KalarickalVineesh Kalarickal
3,017 4 4 gold badges 30 30 silver badges 35 35 bronze badges
0
Add a comment  | 
0
ShareImprove this answerFollowanswered Oct 1 ” 18 at 12 : 59 michal. jakubeczymichal. jakubeczy4, 185 1 1 gold badge 29 29 silver badges 41 41 bronze badgesAdd a comment | $ arrDuplicate = array ( ” “, ” “, 1,3, ” “, 5 ) ; foreach ( array_unique ( $ arrDuplicate ) as USD v ) } print_r ( $ arrRemoved ) ; ShareImprove this answerFollowedited Nov 14 ” 11 at 9 : 15V erbeia4, 370 2 2 gold badges 19 19 silver badges 44 44 bronze badgesanswered Nov 14 ” 11 at 9 : 09 user1045247user104524711This really does not explain anything, and it does not seem to be ” smarter “. – Sven Nov 6 ” 12 at 23 : 24Y ou can always explain why by editing your own post, just click on the ” edit ” link at the end of your answer. I also suggest that you have a look to the FAQ : darkedeneurope.com/faq – ForceMagic Nov 6 ” 12 at 23 : 24A dd a comment | function arrayUnique ( $ myArray ) else if ( ! empty ( $ val2 ) ) } } return ( $ newArray ) ; } function deleteEmpty ( $ myArray ) } return USD retArray ; } ShareImprove this answerFollowedited Nov 6 ” 12 at 23 : 30C hris Forrence9, 633 11 11 gold badges 43 43 silver badges 59 59 bronze badgesanswered Nov 5 ” 12 at 11 : 11V ineesh KalarickalVineesh Kalarickal3, 017 4 4 gold badges 30 30 silver badges 35 35 bronze badgesAdd a comment |try this short và sweet code – $array = array (1,4,2,1,7,4,9,7,5,9); $unique = array(); foreach($array as $v) var_dump($unique); USD array = array ( 1,4,2,1,7,4,9,7,5,9 ) ; USD unique = array ( ) ; foreach ( USD array as USD v ) var_dump ( $ unique ) ;

Output –

array(6)
Share
Improve this answer
Follow
answered Apr 1 “14 at 9:47
Rohit SutharRohit Suthar
3,273 1 1 gold badge 34 34 silver badges 45 45 bronze badges
1
2
Abusing boolean operators for control flow like this is needlessly confusing. Just use if. – Mark Amery Aug 27 “15 at 15:17
Add a comment  | 
0
; print_r(arr_unique($arr1)); function arr_unique($arr) } return $uni_arr; }
Share
Improve this answer
Follow
answered Jan 24 “17 at 8:35
sumityadavbadlisumityadavbadli
211 2 2 silver badges 5 5 bronze badges
1
Putting count($arr) in the loop is quite slow – Kiruahxh May 2 “18 at 8:18
Add a comment  | 
0
array ( 6 ) ShareImprove this answerFollowanswered Apr 1 ” 14 at 9 : 47R ohit SutharRohit Suthar3, 273 1 1 gold badge 34 34 silver badges 45 45 bronze badgesAbusing boolean operators for control flow like this is needlessly confusing. Just use if. – Mark Amery Aug 27 ” 15 at 15 : 17A dd a comment | ; print_r ( arr_unique ( $ arr1 ) ) ; function arr_unique ( $ arr ) } return USD uni_arr ; } ShareImprove this answerFollowanswered Jan 24 ” 17 at 8 : 35 sumityadavbadlisumityadavbadli211 2 2 silver badges 5 5 bronze badgesPutting count ( $ arr ) in the loop is quite slow – Kiruahxh May 2 ” 18 at 8 : 18A dd a comment |Here I ” ve created a second empty array and used for loop with the first array which is having duplicates. It will run as many time as the count of the first array. Then compared with the position of the array with the first array and matched that it has this item already or not by using in_array. If not then it ” ll add that item to second array with array_push. $a = array(1,2,3,1,3,4,5); $count = count($a); $b = <>; for($i=0; $i Share
Improve this answer
Follow
edited Dec 6 “18 at 8:20
answered Dec 4 “18 at 7:31
Aladin BanwalAladin Banwal
1 3 3 bronze badges
3
Can you share some explanation about that code? For example, why don’t you use a foreach loop? – Nico Haase Dec 4 “18 at 9:01
This is a sort form of doing and why I didn’t use foreach because I’m much comfortable with for loop. That’s it. – Aladin Banwal Dec 5 “18 at 2:26
Nevertheless, you should share some explanation with the code. Posting such answers on SO is not a good style: people with the same problem as the OP could come along, and they should be able to understand how your code solves the problem such that they can learn from it and adopt it to their needs – Nico Haase Dec 5 “18 at 6:37
Add a comment  | 
0
USD a = array ( 1,2,3,1,3,4,5 ) ; USD count = count ( $ a ) ; USD b = < > ; for ( USD i = 0 ; USD i ShareImprove this answerFollowedited Dec 6 ” 18 at 8 : 20 answered Dec 4 ” 18 at 7 : 31A ladin BanwalAladin Banwal1 3 3 bronze badgesCan you share some explanation about that code ? For example, why don’t you use a foreach loop ? – Nico Haase Dec 4 ” 18 at 9 : 01T his is a sort form of doing and why I didn’t use foreach because I’m much comfortable with for loop. That’s it. – Aladin Banwal Dec 5 ” 18 at 2 : 26N evertheless, you should share some explanation with the code. Posting such answers on SO is not a good style : people with the same problem as the OP could come along, and they should be able to understand how your code solves the problem such that they can learn from it and adopt it to their needs – Nico Haase Dec 5 ” 18 at 6 : 37A dd a comment |It can be done through function I made three function duplicate returns the values which are duplicate in array. Second function single return only those values which are single mean not repeated in array and third and full function return all values but not duplicated if any value is duplicated it convert it to single ; function duplicate($arr) } return $duplicate; } function single($arr) } return $single; } function full($arr, $arry)
Share
Improve this answer
Follow
edited Dec 7 “18 at 0:04
nyedidikeke
4,840 7 7 gold badges 31 31 silver badges 48 48 bronze badges
answered Jul 27 “15 at 6:45
Mirza ObaidMirza Obaid
1,571 3 3 gold badges 21 21 silver badges 34 34 bronze badges
Add a comment  | 
0
function duplicate ( $ arr ) } return USD duplicate ; } function single ( $ arr ) } return USD single ; } function full ( $ arr, $ arry ) ShareImprove this answerFollowedited Dec 7 ” 18 at 0 : 04 nyedidikeke4, 840 7 7 gold badges 31 31 silver badges 48 48 bronze badgesanswered Jul 27 ” 15 at 6 : 45M irza ObaidMirza Obaid1, 571 3 3 gold badges 21 21 silver badges 34 34 bronze badgesAdd a comment |An alternative for array_unique ( ) function .. Using Brute force algorithm

/ / < 1 > This our array with duplicated items USD matches = ; / / < 2 > Container for the new array without any duplicated items USD arr = < > ; / / <3 > get the length of the duplicated array and set it to the var len to be use for for loop USD len = count ( $ matches ) ; / / < 4 > If matches array key ( USD i ) current loop Iteration is not available in / / < 4 > the array USD arr then push the current iteration key value of the matches / / < 4 > to the array arr. for ( USD i = 0 ; USD i if ( array_search ( $ matches, $ arr ) = = = false ) } / / print the array USD arr. print_r ( $ arr ) ; / / Result : Array ( < 0 > => jorge < 1 > => melvin < 2 > => chelsy <3 > => smith )
Share
Improve this answer
Follow
answered Mar 14 “20 at 19:05
Richard RamosRichard Ramos
41 1 1 silver badge 7 7 bronze badges
Add a comment  | 
0
“302”,”2″=>”302″,”3″=>”276″,”4″=>”301″,”5″=>”302″); print_r(array_values(array_unique($a))); ?>//`output -> Array ( <0> => 302 <1> => 276 <2> => 301 )`
Share
Improve this answer
Follow
answered Jan 1 at 6:01
Majid shiriMajid shiri
1 1 1 bronze badge
Add a comment  | 
-1
ShareImprove this answerFollowanswered Mar 14 ” 20 at 19 : 05R ichard RamosRichard Ramos41 1 1 silver badge 7 7 bronze badgesAdd a comment | ” 302 “, ” 2 ” => ” 302 “, ” 3 ” => ” 276 “, ” 4 ” => ” 301 “, ” 5 ” => ” 302 ” ) ; print_r ( array_values ( array_unique ( $ a ) ) ) ; ? > / / ` output -> Array ( < 0 > => 302 < 1 > => 276 < 2 > => 301 ) ` ShareImprove this answerFollowanswered Jan 1 at 6 : 01M ajid shiriMajid shiri1 1 1 bronze badgeAdd a comment | – 1The array_unique function is just one of the really useful native functions from PHP for dealing with arrays. I recently wrote a piece on them and the spread operator to modifying and manipulating PHP arrays : https://wp-helpers.com/2021/02/27/php-arrays-functions-and-spread-operator-in-wp-context/
Share
Improve this answer
Follow
answered Feb 27 at 12:37
Carlos MatosCarlos Matos
1
1
To use array_unique was answered 12 years ago. – Kim Nyholm Feb 27 at 12:52
Add a comment  | 
-2
ShareImprove this answerFollowanswered Feb 27 at 12 : 37C arlos MatosCarlos MatosTo use array_unique was answered 12 years ago. – Kim Nyholm Feb 27 at 12 : 52A dd a comment | – 2I have done this without using any function. $arr = array(“1”, “2”, “3”, “4”, “5”, “4”, “2”, “1”); $len = count($arr); for ($i = 0; $i < $len; $i++) } } } for ($i = 0; $i < $len; $i++)
Share
Improve this answer
Follow
edited Aug 18 “10 at 12:46
kiamlaluno
24.5k 16 16 gold badges 70 70 silver badges 85 85 bronze badges
answered Mar 7 “10 at 17:31
Ashishdmc4Ashishdmc4
1
0
Add a comment  | 
-2
$array = array(“a” => “moon”, “star”, “b” => “moon”, “star”, “sky”); // Deleting the duplicate items $result = array_unique($array); print_r($result); USD arr = array ( ” 1 “, ” 2 “, ” 3 “, ” 4 “, ” 5 “, ” 4 “, ” 2 “, ” 1 ” ) ; USD len = count ( $ arr ) ; for ( USD i = 0 ; USD i < $ len ; USD i + + ) } } } for ( USD i = 0 ; USD i < $ len ; USD i + + ) ShareImprove this answerFollowedited Aug 18 " 10 at 12 : 46 kiamlaluno24. 5 k 16 16 gold badges 70 70 silver badges 85 85 bronze badgesanswered Mar 7 " 10 at 17 : 31A shishdmc4Ashishdmc4Add a comment | - 2 $ array = array ( " a " => ” moon “, ” star “, ” b ” => ” moon “, ” star “, ” sky ” ) ; / / Deleting the duplicate items USD result = array_unique ( $ array ) ; print_r ( $ result ) ;ref : Demo
Share
Improve this answer
Follow
answered Jul 5 “19 at 13:23
nageen nayaknageen nayak
1,056 2 2 gold badges 15 15 silver badges 21 21 bronze badges
1
3
What’s the point of answering this question with array_unique 11 years after the original answer which is exactly this ? – Dylan Kas Jul 5 “19 at 13:24
Add a comment  | 

Your Answer

ShareImprove this answerFollowanswered Jul 5 ” 19 at 13 : 23 nageen nayaknageen nayak1, 056 2 2 gold badges 15 15 silver badges 21 21 bronze badgesWhat’s the point of answering this question with array_unique 11 years after the original answer which is exactly this ? – Dylan Kas Jul 5 ” 19 at 13 : 24A dd a comment |Thanks for contributing an answer to Stack Overflow !Please be sure to answer the question. Provide details and share your research!Please be sure to answer the question. Provide details and share your research !But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.Asking for help, clarification, or responding to other answers. Making statements based on opinion ; back them up with references or personal experience .To learn more, see our tips on writing great answers.
Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Submit

Post as a guest

Name
Email Draft savedDraft discardedSign up using GoogleSign up using FacebookSign up using E-Mail and PasswordSubmitNameEmailRequired, but never shown

Post as a guest

Name
Email NameEmailRequired, but never shown
Post Your Answer Discard Post Your Answer DiscardBy clicking “ Post Your Answer ”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged php arrays duplicate-data or ask your own question.

The Overflow Blog
Introducing The Key
Podcast 326: What does being a “nerd” even mean these days?
Featured on Meta
Stack Overflow for Teams is now free for up to 50 users, forever
Announcing “The Key™” – copy paste like you’ve never done before
Linked
0
PHP remove values from a single array?
0
How remove duplicate value in PHP
0
How to remove duplicate values in PHP?
-3
Remove same value in associative array
2
How to get top 3 values from array in PHP
0
‘group by’ in an array php
1
Remove duplicate values from an array using PHP
-3
php foreach Loop try not to copy a array
0
Delete array items if repeats more then once, Php
-2
How to combine or merge multiple values inside the single array?
See more linked questions
Related
3760
Create ArrayList from array
1293
Remove empty elements from an array in Javascript
2893
How to append something to an array?
2703
Deleting an element from an array in PHP
2197
How do I empty an array in JavaScript?
1847
Get all unique values in a JavaScript array (remove duplicates)
4688
Reference — What does this symbol mean in PHP?
3001
How to check if an object is an array?
9258
How can I remove a specific item from an array?
4978
For-each over an array in JavaScript
Hot Network Questions
How do the functions Reap and Sow actually work behind the scenes?
Traveling the world without entering countries?
I go round-trip from USA to China and back, friend only goes from China to USA
How can I make my class immune to the “auto value = copy of proxy” landmine in C++?
Taking over another fired developers role
I made a mistake in my predictions for/on/by/in 42 days?
How does a blockchain relying on PoW verify that a hash is computed using an algorithm and not made up by a human?
What are the qualifications for a religious figure to be called “Yati”?
Find the largest number that Bash arithmetic can handle?
Is a neural network essential for deep learning?
to change collation for system database SQL Server 2017
Minimize 1×3 tiles on a 5×5 table to block any more 1×3 tiles
What changed in .net 5 that makes it not throw when changing dictionary values in foreach
Add a QGIS vectorlayer by geometry type
Is it okay to publish less and teach more during your PhD? (CS USA)
How to be a good math teacher at a liberal art college?
Poor man’s JIT using nested lambdas
Is it safe to share .git folder of a public repo?
Can a planet move itself?
How often will this test fail?
How to expand NodeForm so new argument can passed to it
Plants vs Zombies!
What is my environmental poison?
Why is there no blackbody radiation in the high frequency section of Planck’s curve? more hot questions
Question feed
Subscribe to RSS
Question feed The Overflow BlogIntroducing The KeyPodcast 326 : What does being a “ nerd ” even mean these days ? Featured on MetaStack Overflow for Teams is now không lấy phí for up to 50 users, foreverAnnouncing “ The Key ™ ” – copy paste like you’ve never done beforeLinkedPHP remove values from a single array ? How remove duplicate value in PHPHow to remove duplicate values in PHP ? – 3R emove same value in associative arrayHow to get top 3 values from array in PHP’group by ‘ in an array phpRemove duplicate values from an array using PHP-3php foreach Loop try not to copy a arrayDelete array items if repeats more then once, Php-2How to combine or merge multiple values inside the single array ? See more linked questionsRelated3760Create ArrayList from array1293Remove empty elements from an array in Javascript2893How to append something to an array ? 2703D eleting an element from an array in PHP2197How do I empty an array in JavaScript ? 1847G et all unique values in a JavaScript array ( remove duplicates ) 4688R eference — What does this symbol mean in PHP ? 3001H ow to check if an object is an array ? 9258H ow can I remove a specific item from an array ? 4978F or – each over an array in JavaScriptHot Network QuestionsHow do the functions Reap and Sow actually work behind the scenes ? Traveling the world without entering countries ? I go round-trip from USA to Trung Quốc and back, friend only goes from Nước Trung Hoa to USAHow can I make my class immune to the ” auto value = copy of proxy ” landmine in C + + ? Taking over another fired developers roleI made a mistake in my predictions for / on / by / in 42 days ? How does a blockchain relying on PoW verify that a hash is computed using an algorithm and not made up by a human ? What are the qualifications for a religious figure to be called ” Yati ” ? Find the largest number that Bash arithmetic can handle ? Is a neural network essential for deep learning ? to change collation for system database SQL Server 2017M inimize 1 × 3 tiles on a 5 × 5 table to block any more 1 × 3 tilesWhat changed in. net 5 that makes it not throw when changing dictionary values in foreachAdd a QGIS vectorlayer by geometry typeIs it okay to publish less and teach more during your PhD ? ( CS USA ) How to be a good math teacher at a liberal art college ? Poor man’s JIT using nested lambdasIs it safe to share. git thư mục of a public repo ? Can a planet move itself ? How often will this test fail ? How to expand NodeForm so new argument can passed to itPlants vs Zombies ! What is my environmental poison ? Why is there no blackbody radiation in the high frequency section of Planck’s curve ? more hot questionsQuestion feedSubscribe to RSSQuestion feedTo subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-php
Stack Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help Mobile
Products Teams Talent Advertising Enterprise
Company About Press Work Here Legal Privacy Policy Terms of Service Contact Us Cookie Settings Cookie Policy
Stack Exchange
Network Technology Life / Arts Culture / Recreation Science Other
Stack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters Game Development
TeX – LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering
Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica
Salesforce ExpressionEngine® Answers Stack Overflow em Português Blender Network Engineering Cryptography Code Review
Magento Software Recommendations Signal Processing Emacs Raspberry Pi Stack Overflow на русском Code Golf
Stack Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance & Testing Sound Design
Windows Phone more (28)
Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Worldbuilding Video Production
Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia Law Physical Fitness Gardening & Landscaping
Parenting more (10)
English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language
Chinese Language French Language German Language Biblical Hermeneutics History Spanish Language Islam
Русский язык Russian Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Puzzling
Motor Vehicle Maintenance & Repair Board & Card Games Bricks Homebrewing Martial Arts The Great Outdoors Poker
Chess Sports more (16)
MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology
Computer Science Philosophy Linguistics Psychology & Neuroscience Computational Science more (10)
Meta Stack Exchange Stack Apps API Data
Blog Facebook Twitter LinkedIn Instagram lang-phpStack Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help MobileProducts Teams Talent Advertising EnterpriseCompany About Press Work Here Legal Privacy Policy Terms of Service Contact Us Cookie Settings Cookie PolicyStack Exchange Network Technology Life / Arts Culture / Recreation Science OtherStack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters Game DevelopmentTeX – LaTeX Software Engineering Unix và Linux Ask Different ( Apple ) WordPress Development Geographic Information Systems Electrical EngineeringAndroid Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience MathematicaSalesforce ExpressionEngine ® Answers Stack Overflow em Português Blender Network Engineering Cryptography Code ReviewMagento Software Recommendations Signal Processing Emacs Raspberry Pi Stack Overflow на русском Code GolfStack Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance và Testing Sound DesignWindows PhonePhotography Science Fiction và Fantasy Graphic Design Movies và TV Music : Practice và Theory Worldbuilding Video ProductionSeasoned Advice ( cooking ) trang chủ Improvement Personal Finance và Money Academia Law Physical Fitness Gardening và LandscapingParentingEnglish Language và Usage Skeptics Mi Yodeya ( Judaism ) Travel Christianity English Language Learners Japanese LanguageChinese Language French Language German Language Biblical Hermeneutics History Spanish Language IslamРусский язык Russian Language Arqade ( gaming ) Bicycles Role-playing Games Anime và Manga PuzzlingMotor Vehicle Maintenance và Repair Board và Card Games Bricks Homebrewing Martial Arts The Great Outdoors PokerChess SportsMathOverflow Mathematics Cross Validated ( stats ) Theoretical Computer Science Physics Chemistry BiologyComputer Science Philosophy Linguistics Psychology và Neuroscience Computational ScienceMeta Stack Exchange Stack Apps API DataBlog Facebook Twitter LinkedIn Instagramsite design / logo © 2021 Stack Exchange Inc ; user contributions licensed under cc by-sa. rev 2021.4.1.38970
Stack Overflow works best with JavaScript enabled *
Stack Overflow works best with JavaScript enabledYour privacy By clicking “ Accept all cookies ”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Accept all cookies Customize settings
 
Accept all cookies Customize settingsShare
Tweet
Linkedin
Pinterest

Configuring the apache web server to run php on windows

Return json php

CÙNG CHUYÊN MỤC

form register php

Form register php

fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

xda developers forums

Xda developers forums

duyệt mảng trong php

Duyệt mảng trong php

học lập trình php miễn phí

Học lập trình php miễn phí

hướng dẫn sửa cấu hình để chạy dự án laravel từ source code có sẵn

Hướng dẫn sửa cấu hình để chạy dự án laravel từ source code có sẵn

how to install linux, apache, mysql, php (lamp) stack on ubuntu 16

How to install linux, apache, mysql, php (lamp) stack on ubuntu 16