{"id":2211,"date":"2013-02-07T05:44:34","date_gmt":"2013-02-07T05:44:34","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2211"},"modified":"2013-02-07T05:44:34","modified_gmt":"2013-02-07T05:44:34","slug":"php-remove-value-from-array-how-to-remove-value-from-array-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-remove-value-from-array-how-to-remove-value-from-array-examples\/","title":{"rendered":"PHP Remove Value From Array Examples"},"content":{"rendered":"<p>While dealing with <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 in PHP<\/a>, there could be times when you would want to remove a specifc value from array, once you know what that value is. This could help you retain only the data that you want in the array. So do you know how to use <em><strong>PHP to remove value from array<\/strong><\/em>? If not, this article is for you. In this article, I am going to share very easy ways to to remove single value or remove multiple values from array using PHP by means of examples.<\/p>\n<h2><!--more-->How To Remove Value From Array Using PHP<\/h2>\n<p>Let&#8217;s assume that we have an array. For the sake of the example, let&#8217;s assume that we have an array of fruit names. Now we will see how easily we can delete an array value, provided we know the value that we wish to remove. \u00a0Let&#8217;s assume that in the fruits array, we want to remove the array value &#8220;grapes&#8221;.<\/p>\n<h3>Example 1 &#8211; Remove specific value from array by using its value<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 1\">&lt;?php \n\necho 'Example 1: Removing a value from the array&lt;br \/&gt;';\n\n\/\/Declare an array\n$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple');\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 \"grapes\" value from array\nif( ( $key = array_search('grapes', $my_fruits_array) ) !== false ) \n{\n    unset($my_fruits_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_fruits_array);\necho '&lt;\/pre&gt;';\n\n?&gt;<\/pre>\n<h3>Example 2 &#8211; Remove specific value from array using its value<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 2\">&lt;?php \n\necho 'Example 2: Removing a value from the array&lt;br \/&gt;';\n\n$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple');\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 \"grapes\" value from array\n$my_fruits_array = array_diff($my_fruits_array, array('grapes'));\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);\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=2211\" 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!<\/strong><\/p>\n<p>Do you know of any other ways to use <strong><em>PHP to \u00a0remove value from array<\/em><\/strong>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While dealing with arrays in PHP, there could be times when you would want to remove a specifc value from array, once you know what that value is. This could help you retain only the data that you want in the array. So do you know how to use PHP to remove value from array? [&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":[33],"class_list":["post-2211","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-zF","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2211","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=2211"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2211\/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=2211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}