{"id":534,"date":"2023-01-30T19:19:56","date_gmt":"2023-01-30T19:19:56","guid":{"rendered":"https:\/\/mouryasolutions.in\/blog\/?p=534"},"modified":"2023-01-30T19:41:56","modified_gmt":"2023-01-30T19:41:56","slug":"remove-duplicate-array-from-multidimensional-array-in-php","status":"publish","type":"post","link":"https:\/\/mouryasolutions.in\/blog\/remove-duplicate-array-from-multidimensional-array-in-php\/","title":{"rendered":"Remove duplicate array from multidimensional array in php"},"content":{"rendered":"<div class=\"post-content\">\r\n<blockquote class=\"wp-block-quote\">\r\n<p>To remove duplicates from a multidimensional array in PHP, you can use the following approach:<\/p>\r\n<\/blockquote>\r\n\r\n\r\n\r\n<ul>\r\n<li>Convert the multidimensional array into a single-dimensional array using <code>array_map()<\/code> and <code>array_merge()<\/code> functions.<\/li>\r\n\r\n\r\n\r\n<li>Use the <code>array_unique()<\/code> function to remove duplicates from the single-dimensional array.<\/li>\r\n\r\n\r\n\r\n<li>Convert the single-dimensional array back into a multidimensional array using the original array&#8217;s structure.<\/li>\r\n\r\n\r\n\r\n<li>\u00a0<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h4>Here&#8217;s an example code to remove duplicates from a 2-dimensional array:<\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\n$input = array(\r\n  array(1, 2, 3),\r\n  array(2, 3, 4),\r\n  array(3, 4, 5),\r\n  array(1, 2, 3)\r\n);\r\n\r\n\/\/ Convert the input array into a single-dimensional array\r\n$flattened = array_map('serialize', $input);\r\n$flattened = array_unique($flattened);\r\n\r\n\/\/ Convert the single-dimensional array back into a multidimensional array\r\n$result = array_map('unserialize', $flattened);\r\n\r\nprint_r($result);\r\n\r\n?&gt;\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<h4>This will remove any duplicates from the input array, and give you the following output:<\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Array\r\n(\r\n  [0] =&gt; Array\r\n  (\r\n    [0] =&gt; 1\r\n    [1] =&gt; 2\r\n    [2] =&gt; 3\r\n  )\r\n\r\n  [1] =&gt; Array\r\n  (\r\n    [0] =&gt; 2\r\n    [1] =&gt; 3\r\n    [2] =&gt; 4\r\n  )\r\n\r\n  [2] =&gt; Array\r\n  (\r\n    [0] =&gt; 3\r\n    [1] =&gt; 4\r\n    [2] =&gt; 5\r\n  )\r\n)\r\n<\/code><\/pre>\r\n<\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>To remove duplicates from a multidimensional array in PHP, you<a href=\"https:\/\/mouryasolutions.in\/blog\/remove-duplicate-array-from-multidimensional-array-in-php\/\">Read More<i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"><\/i><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/534"}],"collection":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=534"}],"version-history":[{"count":3,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/534\/revisions"}],"predecessor-version":[{"id":541,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/534\/revisions\/541"}],"wp:attachment":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}