{"id":2221,"date":"2013-02-07T06:12:22","date_gmt":"2013-02-07T06:12:22","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2221"},"modified":"2013-02-07T06:12:22","modified_gmt":"2013-02-07T06:12:22","slug":"php-remove-duplicates-from-array-remove-duplicate-values-from-array-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-remove-duplicates-from-array-remove-duplicate-values-from-array-examples\/","title":{"rendered":"How To Remove Duplicates From Array Using PHP"},"content":{"rendered":"<p>While <a title=\"PHP Add To An Array | Add Element To Array Using PHP Examples\" href=\"https:\/\/theextremewebdesigns.com\/blog\/php-add-to-an-array-add-element-to-array-using-php-examples\/\" target=\"_blank\">working with arrays<\/a>, there may have been a time when you came across duplicate values in array. In some cases, these duplicates values are undesirable and you may want to remove these duplicate values from the same array. So do you know how to remove duplicate values from array? If not then here are easy examples that show you how to \u00a0use\u00a0<em><strong>PHP to remove duplicates from array<\/strong><\/em>. <!--more--><\/p>\n<h2>Using PHP to Remove Duplicate Values From Array<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2221\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s consider that we have an array that contains fruits names. One of the fruits is now duplicated in the array and we would like to remove this duplicated value. Consider the following array below:<\/p>\n<pre class=\"lang:php decode:true\" title=\"Sample array with duplicate values\">$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple', 'apple', 'guava', 'apple');<\/pre>\n<p>As you can see in the above array, the fruit &#8220;apple&#8221; appears thrice i.e. it is duplicated twice. So now \u00a0we would like to retain only the first\u00a0occurrence\u00a0of &#8220;apple&#8221; and remove the other two duplicate values from the array.<\/p>\n<h3>Example 1 &#8211; Remove duplicates from array using PHP<\/h3>\n<pre class=\"lang:php decode:true\" title=\"Example 1\">&lt;?php \n\necho 'Example 1: Removing a value from the array - Remove value \"apple\" &lt;br \/&gt;';\n\n\/\/Declare an array\n$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple', 'apple', 'guava', 'apple');\n\n\/\/Print the contents of the array.\necho 'Following are the original array contents:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';\n\n\/\/Remove duplicated \"apple\" value from array\n$my_fruits_array_new = array_unique($my_fruits_array);\n\n\/\/Print the contents of the array.\necho 'Following are the array contents AFTER removing the duplicate values from array:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array_new);\necho '&lt;\/pre&gt;';\n?&gt;<\/pre>\n<h3>Example 2 &#8211; Delete Duplicates From Array Using PHP<\/h3>\n<pre class=\"lang:php decode:true\" title=\"Example 2\">&lt;?php\n\necho '&lt;br \/&gt;Example 2: Removing a value from the array - Remove value \"grapes\" &lt;br \/&gt;';\n\n$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple', 'apple', 'guava', 'apple');\n\n\/\/Print the contents of the array.\necho 'Following are the original array contents:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';\n\n\/\/Remove \"apple\" value from array\n$my_fruits_array_new = array_map('unserialize', array_unique(array_map('serialize', $my_fruits_array)));\n\n\/\/Print the contents of the array. \necho 'Following are the array contents AFTER removing the value from array:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_fruits_array_new);\necho '&lt;\/pre&gt;';\n\n?&gt;<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2221\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h4>That&#8217;s it!<\/h4>\n<p>Do you know of any other ways to use <em><strong>PHP to\u00a0<\/strong><strong>delete duplicate values from array? <\/strong><\/em>Feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working with arrays, there may have been a time when you came across duplicate values in array. In some cases, these duplicates values are undesirable and you may want to remove these duplicate values from the same array. So do you know how to remove duplicate values from array? If not then here are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,13],"tags":[125,33],"class_list":["post-2221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tips-and-tricks","category-tips-and-tricks","tag-php-demos-2","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-zP","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=2221"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2221\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=2221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}