{"id":90,"date":"2011-12-24T15:15:00","date_gmt":"2011-12-24T15:15:00","guid":{"rendered":"https:\/\/www.tech-and-dev.com\/blog\/2011\/12\/24\/searching-and-removing-a-value-from-a-multidimensional-array-in-php\/"},"modified":"2021-02-22T00:55:44","modified_gmt":"2021-02-22T00:55:44","slug":"searching-and-removing-value-from","status":"publish","type":"post","link":"https:\/\/www.tech-and-dev.com\/blog\/2011\/12\/searching-and-removing-value-from.html","title":{"rendered":"Searching and removing a value from a multidimensional array in php"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<p>Recently I found myself in a situation where I needed to search and remove a value from a dynamically generated multidimensional array with a different size and length.<\/p>\n<p>After many trials and errors I found that the best solution is to recursively loop through the array and locate and remove the value.<\/p>\n<p>So I wrote the following function, that takes two parameters, first is the array passed by reference and the second is the value we want to completely remove from the array:<\/p>\n<div style=\"background-color: #eeeeee;\">function recursiveRemoval(&amp;$array, $val)<br \/>\n{<br \/>\nif(is_array($array))<br \/>\n{<br \/>\nforeach($array as $key=&gt;&amp;$arrayElement)<br \/>\n{<br \/>\nif(is_array($arrayElement))<br \/>\n{<br \/>\nrecursiveRemoval($arrayElement, $val);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif($arrayElement == $val)<br \/>\n{<br \/>\nunset($array[$key]);<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/div>\n<p>First thing we&#8217;re doing is checking if the array passed to the function is actually an array.<br \/>\nIf the array passed is an array, we iterate through its elements,<br \/>\nIf the element is also an array (our array is not a one dimensional array), we recursively call the function recursiveRemoval($arrayElement, $val) again to break the multidimensional array into one dimensional arrays.<br \/>\nAt the end, we process the one dimensional array elements, and we compare them to the passed value we want to remove, and we unset the key of the array ($array[$key]).<\/p>\n<p>To download a fully working example, please go to:<br \/>\n<a href=\"http:\/\/www.codeproject.com\/KB\/PHP\/RecursivelyRemoveArrayVal.aspx\">http:\/\/www.codeproject.com\/KB\/PHP\/RecursivelyRemoveArrayVal.aspx<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Recently I found myself in a situation where I needed to search and remove a value from a dynamically generated multidimensional array with a different size and length. After many trials and errors I found that the best solution is to recursively loop through the array and locate and remove the value. So I wrote [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[126,22,109,122,125],"tags":[],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-array","category-php","category-programing","category-recursive","category-search"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":1,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/90\/revisions\/161"}],"wp:attachment":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}