{"id":2251,"date":"2013-02-08T12:40:04","date_gmt":"2013-02-08T12:40:04","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2251"},"modified":"2013-02-08T12:40:04","modified_gmt":"2013-02-08T12:40:04","slug":"php-delete-item-from-array-how-to-delete-item-from-array-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-delete-item-from-array-how-to-delete-item-from-array-examples\/","title":{"rendered":"PHP Delete Item From Array Examples"},"content":{"rendered":"<p><strong><\/strong><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\">Arrays<\/a>, as always, have been a very important concept. While working with arrays, there might have been an instance where you wanted to <strong>delete an array element<\/strong>. In this article, I am going to share with you easy ways to use <em><strong>PHP to delete item from array<\/strong><\/em>\u00a0by means of easy examples.<\/p>\n<p><!--more--><\/p>\n<h2>How To Delete Item 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=2251\" 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 \u00a0assume that we have an array of states. So we will see few examples that satisfy the following requirements:<\/p>\n<p>1. Deleting an Array Element when array consists of unique values.<\/p>\n<p>2. Deleting an Array Element\u00a0when array consists of duplicate values &amp; retain the original value.<\/p>\n<p>3. Deleting an Array Element\u00a0when array consists of duplicate values &amp; delete the original value as well.<\/p>\n<p>4. Deleting an Array Element\u00a0when array consists of multiple repeated values using PHP.\u00a0The original value will be retained, and only the duplicates will be removed.<\/p>\n<p>5. Deleting an Array Element\u00a0when array consists of multiple repeated values using PHP Remove all duplicated values. The original value will be deleted along with the duplicates and the result will be an array with only unique values.<\/p>\n<p>So let&#8217;s get stated with our first case:<\/p>\n<h3>Example 1: Remove Element From Array when array consists of unique values using PHP<\/h3>\n<p><strong>When to use:<\/strong>\u00a0Use only when you know a specific item \/ value and you are sure that it is unique and non-repeated in an array and you want to remove just that specific value.<\/p>\n<pre class=\"lang:php decode:true\" title=\"Example 1\">\/\/Example 1\n\n\/\/Declare an array\n$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska');\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_states_array);\necho '&lt;\/pre&gt;';\n\n\/\/Remove \"Texas\" value from array\nif( ( $key = array_search('Texas', $my_states_array) ) !== false ) \n{\n    unset($my_states_array[$key]);\n}\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_states_array);\necho '&lt;\/pre&gt;';<\/pre>\n<h3>Example 2: Remove Element From Array when array consists of duplicate values using PHP and retain only the original value<\/h3>\n<p><strong>When to use:<\/strong> Use when you know a specific value and you think there might be repeated values of this value and you would want to retain only the original value and delete the rest of the duplicate values.<\/p>\n<pre class=\"lang:php decode:true\" title=\"Example 2\">\/\/Example 2\n\n$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska', 'Texas', 'Utah');\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_states_array);\necho '&lt;\/pre&gt;';\n\n\/\/Remove \"Texas\" value from array, only the ones that are duplicated &amp; retain the original value\n$my_states_array = array_unique($my_states_array);\n\n\/\/Print the contents of the array. \necho 'Following are the array contents AFTER removing the value from array, retaining the original Texas value:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_states_array);\necho '&lt;\/pre&gt;';<\/pre>\n<h3>Example 3: Remove Element From Array when array consists of unique values using PHP and delete the original value as well<\/h3>\n<p><strong>When to use: <\/strong>Use when you want to delete a specific value or item from an array when you think this array might have repeated values for this item. So this way, the duplicated values will be deleted along with original value.<\/p>\n<pre class=\"lang:php decode:true\" title=\" Example 3\">\/\/Example 3\n\n$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska', 'Texas', 'Utah');\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_states_array);\necho '&lt;\/pre&gt;';\n\n\/\/Remove All \"Texas\" value from array including original value\n$my_states_array = array_diff($my_states_array, array('Texas'));\n\n\/\/Print the contents of the array. \necho 'Following are the array contents AFTER removing the value from array, removing even the original Texas value:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_states_array);\necho '&lt;\/pre&gt;';<\/pre>\n<h3>Example 4: Remove Element From Array when array consists of multiple repeated values using PHP and RETAIN the original value<\/h3>\n<p><strong>When to use:<\/strong> Use when you have a dynamic array that has\u00a0multiple repeated values and you don&#8217;t know what these values might be. So if you are not worried about what these values are and don&#8217;t care about them, but you want to retain only unique values plus only one of the duplicated items in the array and delete the duplicated items, then go ahead &amp; use this.<\/p>\n<pre class=\"lang:php decode:true\" title=\"Example 4\">\/\/Example 4\n\n$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska', 'Texas', 'Utah',  'Alaska', 'New York');\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_states_array);\necho '&lt;\/pre&gt;';\n\n\/\/Remove All multiple duplicate values and retain original value\n$my_states_array = array_unique($my_states_array);\n\n\/\/Print the contents of the array. \necho 'Following are the array contents AFTER removing the value from array, removing even the original value:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($my_states_array);\necho '&lt;\/pre&gt;';<\/pre>\n<h3>Example 5: Remove Element From Array when array consists of multiple repeated values using PHP and DELETE the original value<\/h3>\n<p><strong>When to use:<\/strong> Use when you have a dynamic array that has multiple repeated values\u00a0\u00a0and you don&#8217;t know what these values might be. So if you are not worried about what these values are and don&#8217;t care about them, but you want to retain purely only unique values in the array and delete all the duplicated items, then go ahead &amp; use this. So if an array item is repeated at least once, then it will be removed from the result, thereby giving you only pure, non-repeating values.<\/p>\n<pre class=\"lang:php decode:true\" title=\"Example 5\">\/\/Example 5\n\n$my_states_array = array('Texas', 'Alabama', 'New York', 'Alaska', 'Texas', 'Utah',  'Alaska', 'New York');\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_states_array);\necho '&lt;\/pre&gt;';\n\n\/\/First get all unique values of array\n$unique_array = array_unique($my_states_array);\n\necho 'Following are the array contents AFTER applying array_unique function. We get only the unique values and this will leave out multiple, repeated values&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($unique_array);\necho '&lt;\/pre&gt;';\n\n\/\/Compute the difference of the original array and the unique array and return an array containing all the values from $my_states_array that are not present in the other arrays ($unique_array). So this means that only the unique, non-repeated values will be left out and the resultant array will contain values that were repeated at least once.\n$compared_array = array_diff_assoc($my_states_array, $unique_array);\n\n\/\/Print the contents of the compared_array. \necho 'Following are the array contents AFTER applying array_diff_assoc. This will leave the most unique, non-repeated values and will return an array of values that were repeated at least once:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($compared_array);\necho '&lt;\/pre&gt;';\n\n\/\/Apply array_diff to compute difference of two arrays and return the difference i.e  it returns an array containing all the entries from my_states_array that are not present in any of the other arrays i.e. $compared_array.\n$all_unique_non_repeated_values = array_diff($my_states_array, $compared_array);\n\necho 'Following are the array contents AFTER applying array_diff. This will leave the most unique, non-repeated values and will return an array of values that were repeated at least once. So the following is our FINAL DESIRED RESULT:&lt;br \/&gt;';\necho '&lt;pre&gt;';\nprint_r($all_unique_non_repeated_values);\necho '&lt;\/pre&gt;';\necho '&lt;br \/&gt;\/*=============================================================================*\/&lt;br \/&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=2251\" 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><strong>That&#8217;s it!<br \/>\n<\/strong>Do you know of any other ways to use\u00a0<strong><em>PHP to delete item from array<\/em>?<\/strong> Feel free to share by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arrays, as always, have been a very important concept. While working with arrays, there might have been an instance where you wanted to delete an array element. In this article, I am going to share with you easy ways to use PHP to delete item from array\u00a0by means of easy examples.<\/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":[33],"class_list":["post-2251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tips-and-tricks","category-tips-and-tricks","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-Aj","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2251","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=2251"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2251\/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=2251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}