{"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 >= 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[pastacode lang=\u201dphp\u201d manual=\u201darray%20array_map%20(%20callable%20%24callback%20%2C%20array%20%24array1%20%5B%2C%20array%20%24\u2026%20%5D%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0Afunction%20cube(%24n)%0A%7B%0A%20%20%20%20return(%24n%20*%20%24n%20*%20%24n)%3B%0A%7D%0A%0A%24a%20%3D%20array(1%2C%202%2C%203%2C%204%2C%205)%3B%0A%24b%20%3D%20array_map(%22cube%22%2C%20%24a)%3B%0Aprint_r(%24b)%3B%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"this-makes-b-have\"><span style=\"color: #000000;\">This makes $b have:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dArray%0A(%0A%20%20%20%20%5B0%5D%20%3D%3E%201%0A%20%20%20%20%5B1%5D%20%3D%3E%208%0A%20%20%20%20%5B2%5D%20%3D%3E%2027%0A%20%20%20%20%5B3%5D%20%3D%3E%2064%0A%20%20%20%20%5B4%5D%20%3D%3E%20125%0A)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h2 id=\"array_filter\"><span style=\"color: #993366;\">array_filter<\/span><\/h2>\n<p>(PHP 4 >= 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[pastacode lang=\u201dphp\u201d manual=\u201darray%20array_filter%20(%20array%20%24array%20%5B%2C%20callable%20%24callback%20%5B%2C%20int%20%24flag%20%3D%200%20%5D%5D%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%0A%24entry%20%3D%20array(%0A%20%20%20%20%20%20%20%20%20%20%20%20%200%20%3D%3E%20\u2019foo\u2019%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%201%20%3D%3E%20false%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%202%20%3D%3E%20-1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%203%20%3D%3E%20null%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%204%20%3D%3E%20\u201d%0A%20%20%20%20%20%20%20%20%20%20)%3B%0A%0Aprint_r(array_filter(%24entry))%3B%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h4 id=\"the-above-example-will-output\"><span style=\"color: #000000;\">The above example will output:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dArray%0A(%0A%20%20%20%20%5B0%5D%20%3D%3E%20foo%0A%20%20%20%20%5B2%5D%20%3D%3E%20-1%0A)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dphp\u201d manual=\u201dbool%20array_walk%20(%20array%20%26%24array%20%2C%20callable%20%24callback%20%5B%2C%20mixed%20%24userdata%20%3D%20NULL%20%5D%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%20%0Afunction%20odd(%24var)%20%7B%0A%20%20%20%20%2F%2F%20returns%20whether%20the%20input%20integer%20is%20odd%0A%20%20%20%20return(%24var%20%26%201)%3B%0A%7D%0A%20%0Afunction%20even(%24var)%20%7B%0A%20%20%20%20%2F%2F%20returns%20whether%20the%20input%20integer%20is%20even%0A%20%20%20%20return(!(%24var%20%26%201))%3B%0A%7D%0A%20%0A%24array1%20%3D%20array(%22a%22%20%3D%3E%201%2C%20%22b%22%20%3D%3E%202%2C%20%22c%22%20%3D%3E%203%2C%20%22d%22%20%3D%3E%204%2C%20%22e%22%20%3D%3E%205)%3B%0A%24array2%20%3D%20array(6%2C%207%2C%208%2C%209%2C%2010%2C%2011%2C%2012)%3B%0A%20%0Aecho%20%22Odd%20%3A%5Cn%22%3B%0Aprint_r(array_filter(%24array1%2C%20%22odd%22))%3B%0Aecho%20%22Even%3A%5Cn%22%3B%0Aprint_r(array_filter(%24array2%2C%20%22even%22))%3B%0A%20%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>The idea of mapping an function to array of data comes from functional programming.<\/p>\n<ul>\n<li>You shouldn\u2019t 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\u2019t).<\/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 (&$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 \u201cchange\u201d the data in-place vs returning a new \u201cchanged\u201d 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 >= 4.0.6, PHP 5, PHP 7) array_map \u2014 Applies the callback to the elements of the given arrays Description: [pastacode lang=\u201dphp\u201d manual=\u201darray%20array_map%20(%20callable%20%24callback%20%2C%20array%20%24array1%20%5B%2C%20array%20%24\u2026%20%5D%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] 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 [&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}]}}