{"id":2103,"date":"2017-03-24T18:20:04","date_gmt":"2017-03-24T12:50:04","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=2103"},"modified":"2018-10-24T11:53:21","modified_gmt":"2018-10-24T06:23:21","slug":"difference-array_map-array_walk-array_filter","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/difference-array_map-array_walk-array_filter\/","title":{"rendered":"Difference between array_map, array_walk and array_filter"},"content":{"rendered":"<h2 id=\"array_map\"><span style=\"color: #800080;\">array_map:<\/span><\/h2>\n<p>(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7)<br \/>\narray_map \u2014 Applies the callback to the elements of the given arrays<\/p>\n<h3 id=\"description\"><span style=\"color: #003300;\">Description:<\/span><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">array array_map ( callable $callback , array $array1 [, array $... ] )<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>array_map() returns an <a href=\"https:\/\/www.wikitechy.com\/technology\/c-programming-check-array-can-divided-pairs-whose-sum-divisible-k\/\">array<\/a> containing all the elements of array1 after applying the <a href=\"https:\/\/www.wikitechy.com\/step-by-step-tutorials\/jquery\/jquery-set-html-call-back-function\">callback function<\/a> to each one.<\/p>\n<p>The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()<\/p>\n<h3 id=\"example-1\"><span style=\"color: #800080;\">Example #1<\/span><\/h3>\n<h4 id=\"array_map-example\"><strong><span style=\"color: #339966;\">array_map() example:<\/span><\/strong><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>function cube($n)<br\/>{<br\/>    return($n * $n * $n);<br\/>}<br\/><br\/>$a = array(1, 2, 3, 4, 5);<br\/>$b = array_map(&quot;cube&quot;, $a);<br\/>print_r($b);<br\/>?&gt;<\/code><\/pre> <\/div>\n<h4 id=\"this-makes-b-have\"><span style=\"color: #000000;\">This makes $b have:<\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">Array<br\/>(<br\/>    [0] =&gt; 1<br\/>    [1] =&gt; 8<br\/>    [2] =&gt; 27<br\/>    [3] =&gt; 64<br\/>    [4] =&gt; 125<br\/>)<\/code><\/pre> <\/div>\n<h2 id=\"array_filter\"><span style=\"color: #993366;\">array_filter<\/span><\/h2>\n<p>(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7)<br \/>\narray_filter \u2014 Filters elements of an array using a callback function<\/p>\n<h3 id=\"description-2\"><span style=\"color: #003300;\">Description<\/span><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )<\/code><\/pre> <\/div>\n<p>Iterates over each value in the array passing them to the callback function.<\/p>\n<p>If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.<\/p>\n<h3 id=\"example-2\"><span style=\"color: #800080;\">Example #2<\/span><\/h3>\n<p><span style=\"color: #339966;\"><strong>array_filter() without callback<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/><br\/>$entry = array(<br\/>             0 =&gt; &#039;foo&#039;,<br\/>             1 =&gt; false,<br\/>             2 =&gt; -1,<br\/>             3 =&gt; null,<br\/>             4 =&gt; &#039;&#039;<br\/>          );<br\/><br\/>print_r(array_filter($entry));<br\/>?&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"the-above-example-will-output\"><span style=\"color: #000000;\">The above example will output:<\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">Array<br\/>(<br\/>    [0] =&gt; foo<br\/>    [2] =&gt; -1<br\/>)<\/code><\/pre> <\/div>\n<h2 id=\"array_walk\"><span style=\"color: #0000ff;\">array_walk<\/span><\/h2>\n<p>(PHP 4, PHP 5, PHP 7)<br \/>\narray_walk \u2014 Apply a user supplied function to every member of an array<\/p>\n<h3 id=\"description-3\"><span style=\"color: #003300;\">Description :<\/span><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">bool array_walk ( array &amp;$array , callable $callback [, mixed $userdata = NULL ] )<\/code><\/pre> <\/div>\n<ul>\n<li>Applies the user-defined callback function to each element of the array array.<\/li>\n<li>array_walk() is not affected by the internal array <a href=\"https:\/\/www.wikitechy.com\/technology\/c-program-to-add-two-numbers-using-pointers\/\">pointer<\/a> of array.<\/li>\n<li>array_walk() will walk through the entire array regardless of pointer position.<\/li>\n<\/ul>\n<h3 id=\"example-1-2\"><span style=\"color: #800080;\">Example #1\u00a0<\/span><\/h3>\n<p><span style=\"color: #339966;\"><strong>array_walk()\u00a0example<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/> <br\/>function odd($var) {<br\/>    \/\/ returns whether the input integer is odd<br\/>    return($var &amp; 1);<br\/>}<br\/> <br\/>function even($var) {<br\/>    \/\/ returns whether the input integer is even<br\/>    return(!($var &amp; 1));<br\/>}<br\/> <br\/>$array1 = array(&quot;a&quot; =&gt; 1, &quot;b&quot; =&gt; 2, &quot;c&quot; =&gt; 3, &quot;d&quot; =&gt; 4, &quot;e&quot; =&gt; 5);<br\/>$array2 = array(6, 7, 8, 9, 10, 11, 12);<br\/> <br\/>echo &quot;Odd :\\n&quot;;<br\/>print_r(array_filter($array1, &quot;odd&quot;));<br\/>echo &quot;Even:\\n&quot;;<br\/>print_r(array_filter($array2, &quot;even&quot;));<br\/> <br\/>?&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>The idea of mapping an function to array of data comes from functional programming.<\/p>\n<ul>\n<li>You shouldn&#8217;t think about array_map as a <a href=\"https:\/\/www.wikitechy.com\/technology\/find-foreach-index\/\">foreach loop<\/a> that calls a function on each element of the array.<\/li>\n<li>It should be thought of as applying the function to each element in the array independently.<\/li>\n<\/ul>\n<p>In theory such things as function mapping can be done in parallel since the function being applied to the data should ONLY effect the data and NOT the global state.<\/p>\n<ul>\n<li>This is because an array_map could choose any order in which to apply the function to the items in (even though in PHP it doesn&#8217;t).<\/li>\n<\/ul>\n<p>array_walk on the other hand it the exact opposite approach to handling arrays of data.<\/p>\n<ul>\n<li>Instead of handling each item separately, it uses a state (&amp;$userdata) and can edit the item in place (much like a foreach loop).<\/li>\n<li>Since each time an item has the $funcname applied to it, it could change the global state of the program and therefor requires a single correct way of processing the items.<\/li>\n<\/ul>\n<p>Back in <a href=\"https:\/\/www.wikitechy.com\/technology\/read-file-line-line-php\/\">PHP<\/a> land, array_map and array_walk are almost identical except array_walk gives you more control over the iteration of data and is normally used to &#8220;change&#8221; the data in-place vs returning a new &#8220;changed&#8221; array.<\/p>\n<p>array_filter is really an application of array_walk (or array_reduce) and it more-or-less just provided for convenience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>array_map: (PHP 4 &gt;= 4.0.6, PHP 5, PHP 7) array_map \u2014 Applies the callback to the elements of the given arrays Description: [ad type=&#8221;banner&#8221;] array_map() returns an array containing all the elements of array1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[2628,4494,4495,4496,4493,4500,4497,4501,4503,3421,1204,3422,4498,2627,404,4499,2626,4502],"class_list":["post-2103","post","type-post","status-publish","format-standard","hentry","category-php","tag-and-protected","tag-array_filter","tag-array_map","tag-array_walk","tag-array_walk-or-array_map","tag-array_walk-vs-array_map-vs-foreach","tag-call","tag-difference-between-array_filter-and-array_map","tag-difference-between-array_walk-and-array_walk_recursive-in-php","tag-difference-between-require","tag-how-does-php-foreach-actually-work","tag-include-and-require_once","tag-pass-parameter","tag-private","tag-reference-what-does-this-symbol-mean-in-php","tag-return","tag-what-is-the-difference-between-public","tag-why-does-the-function-signature-differ-between-array_map-and-array_filterarray_reduce"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/2103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=2103"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/2103\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=2103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=2103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=2103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}