{"id":1086,"date":"2012-10-21T18:11:01","date_gmt":"2012-10-22T01:11:01","guid":{"rendered":"http:\/\/www.virendrachandak.com\/techtalk\/\/?p=1086"},"modified":"2016-03-20T12:22:03","modified_gmt":"2016-03-20T19:22:03","slug":"how-to-apply-a-function-to-every-array-element-in-php","status":"publish","type":"post","link":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/","title":{"rendered":"How to apply a function to every array element in PHP"},"content":{"rendered":"<div>I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input, function like htmlentities can be used. We can easily use this function on a single variable or a single dimension array by simply applying the function or doing a foreach loop respectively. However, to do that on a multi-dimensional array is not very easy using foreach loop. In this article I will discuss the various methods we can use to apply the htmlentities function to a single variable and different types of arrays. The methods that I will discuss can be used to apply any default PHP function or any user defined function to every element of an array.<\/div>\n<p><!--more--><\/p>\n<div>Here are the various example of how we can apply the function on different inputs along with the output of the variable after the function is applied. In all these examples I will use htmlentities functions as an example function, as it is easy to identify if the function worked or not. htmlentities function converts all applicable characters to HTML entities. In the following examples if the &#8216;&lt; &#8216; and &#8216;&gt;&#8217; are converted into &#8216;&lt;&#8216; and &#8216;&gt;&#8217; respectively, that means the function got applied correctly.<\/div>\n<div style=\"padding-top: 15px;\">\n<h3>Apply htmlentites to the value of a variable:<\/h3>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n$var = 'some value is &lt;strong&gt;bold&lt;\/strong&gt;';\r\n$var = htmlentities($var);\r\nvar_dump($var);\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; notranslate\" title=\"\">\r\nstring(47) &quot;some value is &amp;lt;strong&amp;gt;bold&amp;lt;\/strong&amp;gt;&quot;\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 15px;\">\n<h3>Apply htmlentities to single dimensional array:<\/h3>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n$array = array();\r\n$array&#x5B;'key1'] = 'value &lt;strong&gt;1&lt;\/strong&gt;';\r\n$array&#x5B;'key2'] = 'value &lt;strong&gt;2&lt;\/strong&gt;';\r\nforeach($array as $key =&gt; $val)\r\n{\r\n\t$array&#x5B;$key] = htmlentities($val);\r\n}\r\nvar_dump($array);\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; notranslate\" title=\"\">\r\narray(2) {\r\n  &#x5B;&quot;key1&quot;]=&gt;\r\n  string(36) &quot;value &amp;lt;strong&amp;gt;1&amp;lt;\/strong&amp;gt;&quot;\r\n  &#x5B;&quot;key2&quot;]=&gt;\r\n  string(36) &quot;value &amp;lt;strong&amp;gt;2&amp;lt;\/strong&amp;gt;&quot;\r\n}\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 15px;\">\n<h3>Apply htmlentities to two-dimensional array.<\/h3>\n<p><span class=\"text-decoration:underline;\">Method 1:<\/span> Use array_map function. This method works only if the array is always a two-dimensional array.<\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n$array = array();\r\n$array&#x5B;'key1']&#x5B;'key2'] = 'value &lt;strong&gt;1&lt;\/strong&gt;';\r\n$array&#x5B;'key1']&#x5B;'key3'] = 'value &lt;strong&gt;2&lt;\/strong&gt;';\r\nforeach($array as &amp;$val)\r\n{\r\n\t$val = array_map('htmlentities', $val);\r\n}\r\nvar_dump($array);\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; notranslate\" title=\"\">\r\narray(1) {\r\n  &#x5B;&quot;key1&quot;]=&gt;\r\n  &amp;array(2) {\r\n    &#x5B;&quot;key2&quot;]=&gt;\r\n    string(36) &quot;value &amp;lt;strong&amp;gt;1&amp;lt;\/strong&amp;gt;&quot;\r\n    &#x5B;&quot;key3&quot;]=&gt;\r\n    string(36) &quot;value &amp;lt;strong&amp;gt;2&amp;lt;\/strong&amp;gt;&quot;\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 15px;\"><span class=\"text-decoration:underline;\">Method 2:<\/span> There is another way to apply htmlentities to a two-dimensional array without using foreach loop.<\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n$array = array();\r\n$array&#x5B;'key1']&#x5B;'key2'] = 'value &lt;strong&gt;1&lt;\/strong&gt;';\r\n$array&#x5B;'key1']&#x5B;'key3'] = 'value &lt;strong&gt;2&lt;\/strong&gt;';\r\narray_walk_recursive($array, function (&amp;$value) {\r\n\t$value = htmlentities($value);\r\n});\r\nvar_dump($array);\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; notranslate\" title=\"\">\r\narray(1) {\r\n  &#x5B;&quot;key1&quot;]=&gt;\r\n  array(2) {\r\n    &#x5B;&quot;key2&quot;]=&gt;\r\n    string(36) &quot;value &amp;lt;strong&amp;gt;1&amp;lt;\/strong&amp;gt;&quot;\r\n    &#x5B;&quot;key3&quot;]=&gt;\r\n    string(36) &quot;value &amp;lt;strong&amp;gt;2&amp;lt;\/strong&amp;gt;&quot;\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 15px;\">\n<h3>Apply htmlentites to multidimensional array:<\/h3>\n<p>In the method 1 to apply the function to a 2-dimensional array, I have used array_map function. However, array_map does not works with multidimensional arrays. Also, array_map does not works recursively hence we need a loop. Let&#8217;s see what we will get if we try to use this method on a multidimensional array:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$array = array();\r\n$array&#x5B;'key1']&#x5B;'key11'] = 'value &lt;strong&gt;11&lt;\/strong&gt;';\r\n$array&#x5B;'key1']&#x5B;'key12'] = 'value &lt;strong&gt;12&lt;\/strong&gt;';\r\n$array&#x5B;'key2']&#x5B;'key13'] = 'value &lt;strong&gt;13&lt;\/strong&gt;';\r\n$array&#x5B;'key2'] = 'value &lt;strong&gt;21&lt;\/strong&gt;';\r\n$array&#x5B;'key3']&#x5B;'key31']&#x5B;'key311'] = 'value &lt;strong&gt;311&lt;\/strong&gt;';\r\n\r\nforeach($array as &amp;$val)\r\n{\r\n\t$val = array_map('htmlentities', $val);\r\n}\r\nvar_dump($array);\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; notranslate\" title=\"\">\r\nWarning:  array_map() &#x5B;function.array-map]: Argument #2 should be an array in test.php on line 10\r\nWarning:  htmlentities() expects parameter 1 to be string, array given in test.php on line 10\r\narray(3) {\r\n  &#x5B;&quot;key1&quot;]=&gt;\r\n  array(2) {\r\n    &#x5B;&quot;key11&quot;]=&gt;\r\n    string(37) &quot;value &amp;lt;strong&amp;gt;11&amp;lt;\/strong&amp;gt;&quot;\r\n    &#x5B;&quot;key12&quot;]=&gt;\r\n    string(37) &quot;value &amp;lt;strong&amp;gt;12&amp;lt;\/strong&amp;gt;&quot;\r\n  }\r\n  &#x5B;&quot;key2&quot;]=&gt;\r\n  NULL\r\n  &#x5B;&quot;key3&quot;]=&gt;\r\n  &amp;array(1) {\r\n    &#x5B;&quot;key31&quot;]=&gt;\r\n    NULL\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 15px;\">As we saw in the above example, we didn&#8217;t get the expected output and also got couple warnings. It didn&#8217;t work recursively on all elements of the array. Hence, if you are not sure about the dimensions of the array or if the array has different dimensions at each level then you cannot use array_map. In this case we can use the array_walk_recursive function. The array_walk_recursive function works recursively on the array and hence we don&#8217;t need any for loop for it.<\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n$array = array();\r\n$array&#x5B;'key1']&#x5B;'key11'] = 'value &lt;strong&gt;11&lt;\/strong&gt;';\r\n$array&#x5B;'key1']&#x5B;'key12'] = 'value &lt;strong&gt;12&lt;\/strong&gt;';\r\n$array&#x5B;'key2']&#x5B;'key13'] = 'value &lt;strong&gt;13&lt;\/strong&gt;';\r\n$array&#x5B;'key2'] = 'value &lt;strong&gt;21&lt;\/strong&gt;';\r\n$array&#x5B;'key3']&#x5B;'key31']&#x5B;'key311'] = 'value &lt;strong&gt;311&lt;\/strong&gt;';\r\narray_walk_recursive($array, function (&amp;$value) {\r\n\t$value = htmlentities($value);\r\n});\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; notranslate\" title=\"\">\r\narray(3) {\r\n  &#x5B;&quot;key1&quot;]=&gt;\r\n  array(2) {\r\n    &#x5B;&quot;key11&quot;]=&gt;\r\n    string(37) &quot;value &amp;lt;strong&amp;gt;11&amp;lt;\/strong&amp;gt;&quot;\r\n    &#x5B;&quot;key12&quot;]=&gt;\r\n    string(37) &quot;value &amp;lt;strong&amp;gt;12&amp;lt;\/strong&amp;gt;&quot;\r\n  }\r\n  &#x5B;&quot;key2&quot;]=&gt;\r\n  string(37) &quot;value &amp;lt;strong&amp;gt;21&amp;lt;\/strong&amp;gt;&quot;\r\n  &#x5B;&quot;key3&quot;]=&gt;\r\n  array(1) {\r\n    &#x5B;&quot;key31&quot;]=&gt;\r\n    array(1) {\r\n      &#x5B;&quot;key311&quot;]=&gt;\r\n      string(38) &quot;value &amp;lt;strong&amp;gt;311&amp;lt;\/strong&amp;gt;&quot;\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 15px;\"><strong>Note<\/strong>: All the examples above work on PHP 5.3+. They won&#8217;t work in PHP versions less than PHP 5.3.0, because they do not support anonymous functions. So to make it work with PHP version &lt; 5.3.0 the above code can be modified as follows:<\/p>\n<pre class=\"brush: php; gutter: false; title: ; notranslate\" title=\"\">\r\n$array = array();\r\n$array&#x5B;'key1']&#x5B;'key11'] = 'value &lt;strong&gt;11&lt;\/strong&gt;';\r\n$array&#x5B;'key1']&#x5B;'key12'] = 'value &lt;strong&gt;12&lt;\/strong&gt;';\r\n$array&#x5B;'key2']&#x5B;'key13'] = 'value &lt;strong&gt;13&lt;\/strong&gt;';\r\n$array&#x5B;'key2'] = 'value &lt;strong&gt;21&lt;\/strong&gt;';\r\n$array&#x5B;'key3']&#x5B;'key31']&#x5B;'key311'] = 'value &lt;strong&gt;311&lt;\/strong&gt;';\r\nfunction my_apply_htmlentities(&amp;$value)\r\n{\r\n\t$value = htmlentities($value);\r\n}\r\narray_walk_recursive($array, 'my_apply_htmlentities');\r\nvar_dump($array);\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; notranslate\" title=\"\">\r\narray(3) {\r\n  &#x5B;&quot;key1&quot;]=&gt;\r\n  array(2) {\r\n    &#x5B;&quot;key11&quot;]=&gt;\r\n    string(37) &quot;value &amp;lt;strong&amp;gt;11&amp;lt;\/strong&amp;gt;&quot;\r\n    &#x5B;&quot;key12&quot;]=&gt;\r\n    string(37) &quot;value &amp;lt;strong&amp;gt;12&amp;lt;\/strong&amp;gt;&quot;\r\n  }\r\n  &#x5B;&quot;key2&quot;]=&gt;\r\n  string(37) &quot;value &amp;lt;strong&amp;gt;21&amp;lt;\/strong&amp;gt;&quot;\r\n  &#x5B;&quot;key3&quot;]=&gt;\r\n  array(1) {\r\n    &#x5B;&quot;key31&quot;]=&gt;\r\n    array(1) {\r\n      &#x5B;&quot;key311&quot;]=&gt;\r\n      string(38) &quot;value &amp;lt;strong&amp;gt;311&amp;lt;\/strong&amp;gt;&quot;\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 15px;\"><strong>Note<\/strong>: I have tested all the above codes and all codes (except for the last) work on PHP 5.3.13 and the last one works on PHP 5.2.11<\/div>\n<div style=\"padding-top: 15px;\">\n<p><strong>Related Links<\/strong>:<\/p>\n<ul>\n<li><a title=\"array_walk_recursive - PHP Manual\" href=\"http:\/\/www.php.net\/manual\/en\/function.array-walk-recursive.php\" rel=\"external nofollow\" target=\"_blank\">array_walk_recursive &#8211; PHP Manual<\/a><\/li>\n<li><a title=\"array_map - PHP Manual\" href=\"http:\/\/www.php.net\/manual\/en\/function.array-map.php\" rel=\"external nofollow\" target=\"_blank\">array_map &#8211; PHP Manual<\/a><\/li>\n<li><a title=\"Anonymous functions - PHP Manual\" href=\"http:\/\/www.php.net\/manual\/en\/functions.anonymous.php\" rel=\"external nofollow\" target=\"_blank\">Anonymous functions &#8211; PHP Manual<\/a><\/li>\n<li><a title=\"Stackoverflow question - array_map and htmlentities\" href=\"http:\/\/stackoverflow.com\/questions\/9150442\/array-map-and-htmlentities\" rel=\"external nofollow\" target=\"_blank\">Stackoverflow question &#8211; array_map and htmlentities<\/a><\/li>\n<li><a title=\"How Would I Apply htmlentities() To Every Array Item\" href=\"http:\/\/www.talkphp.com\/advanced-php-programming\/1886-how-would-i-apply-htmlentities-every-array-item.html\" rel=\"external nofollow\" target=\"_blank\">How Would I Apply htmlentities() To Every Array Item<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input, function like htmlentities can be used. We can easily use this function on a single variable or a single dimension array by simply applying the function or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[143,8],"tags":[109,108,124],"class_list":["post-1086","post","type-post","status-publish","format-standard","hentry","category-php","category-web-development","tag-array_map","tag-array_walk_recursive","tag-snippets"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to apply a function to every array element in PHP - Virendra&#039;s TechTalk<\/title>\n<meta name=\"description\" content=\"I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to apply a function to every array element in PHP - Virendra&#039;s TechTalk\" \/>\n<meta property=\"og:description\" content=\"I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Virendra&#039;s TechTalk\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:published_time\" content=\"2012-10-22T01:11:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-03-20T19:22:03+00:00\" \/>\n<meta name=\"author\" content=\"Virendra Chandak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:site\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virendra Chandak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/\"},\"author\":{\"name\":\"Virendra Chandak\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"headline\":\"How to apply a function to every array element in PHP\",\"datePublished\":\"2012-10-22T01:11:01+00:00\",\"dateModified\":\"2016-03-20T19:22:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/\"},\"wordCount\":1335,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"keywords\":[\"array_map\",\"array_walk_recursive\",\"snippets\"],\"articleSection\":[\"PHP\",\"Web Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/\",\"url\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/\",\"name\":\"How to apply a function to every array element in PHP - Virendra's TechTalk\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#website\"},\"datePublished\":\"2012-10-22T01:11:01+00:00\",\"dateModified\":\"2016-03-20T19:22:03+00:00\",\"description\":\"I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input,\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-apply-a-function-to-every-array-element-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"TechTalk\",\"item\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/category\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to apply a function to every array element in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#website\",\"url\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/\",\"name\":\"Virendra's TechTalk\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\",\"name\":\"Virendra Chandak\",\"logo\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.virendrachandak.com\",\"https:\\\/\\\/www.facebook.com\\\/virendrachandak\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/virendrachandak\\\/\",\"https:\\\/\\\/x.com\\\/virendrachandak\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to apply a function to every array element in PHP - Virendra's TechTalk","description":"I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/","og_locale":"en_US","og_type":"article","og_title":"How to apply a function to every array element in PHP - Virendra's TechTalk","og_description":"I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input,","og_url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/","og_site_name":"Virendra&#039;s TechTalk","article_publisher":"https:\/\/www.facebook.com\/virendrachandak","article_author":"https:\/\/www.facebook.com\/virendrachandak","article_published_time":"2012-10-22T01:11:01+00:00","article_modified_time":"2016-03-20T19:22:03+00:00","author":"Virendra Chandak","twitter_card":"summary_large_image","twitter_creator":"@virendrachandak","twitter_site":"@virendrachandak","twitter_misc":{"Written by":"Virendra Chandak","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/#article","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/"},"author":{"name":"Virendra Chandak","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"headline":"How to apply a function to every array element in PHP","datePublished":"2012-10-22T01:11:01+00:00","dateModified":"2016-03-20T19:22:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/"},"wordCount":1335,"commentCount":4,"publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"keywords":["array_map","array_walk_recursive","snippets"],"articleSection":["PHP","Web Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/","url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/","name":"How to apply a function to every array element in PHP - Virendra's TechTalk","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website"},"datePublished":"2012-10-22T01:11:01+00:00","dateModified":"2016-03-20T19:22:03+00:00","description":"I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input,","breadcrumb":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-apply-a-function-to-every-array-element-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"TechTalk","item":"https:\/\/www.virendrachandak.com\/techtalk\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},{"@type":"ListItem","position":3,"name":"How to apply a function to every array element in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website","url":"https:\/\/www.virendrachandak.com\/techtalk\/","name":"Virendra's TechTalk","description":"","publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.virendrachandak.com\/techtalk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17","name":"Virendra Chandak","logo":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.virendrachandak.com","https:\/\/www.facebook.com\/virendrachandak","https:\/\/www.linkedin.com\/in\/virendrachandak\/","https:\/\/x.com\/virendrachandak"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2vTtQ-hw","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1972,"url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-sort-a-multi-dimension-array-by-value-in-php\/","url_meta":{"origin":1086,"position":0},"title":"How to sort a multi-dimension array by value in PHP","author":"Virendra Chandak","date":"February 16, 2015","format":false,"excerpt":"In this article we will see how to sort a multi-dimension array by value of one of the keys of the array. We can use a few different methods to do this. One way to to use usort() function. Another way is to just identify the values and create another\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":688,"url":"https:\/\/www.virendrachandak.com\/techtalk\/php-isset-vs-empty-vs-is_null\/","url_meta":{"origin":1086,"position":1},"title":"PHP isset() vs empty() vs is_null()","author":"Virendra Chandak","date":"January 21, 2012","format":false,"excerpt":"PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results. isset() and empty() are\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1714,"url":"https:\/\/www.virendrachandak.com\/techtalk\/php-5-5-password-hashing-api\/","url_meta":{"origin":1086,"position":2},"title":"PHP 5.5 Password Hashing API","author":"Virendra Chandak","date":"June 1, 2014","format":false,"excerpt":"Most of the applications or websites today have a user registration system which requires storing usernames, passwords etc. A developer of the application should always store passwords securely and never in plain text. There are many methods to encrypt or hash passwords and store in the database but which method\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":450,"url":"https:\/\/www.virendrachandak.com\/techtalk\/getting-real-client-ip-address-in-php-2\/","url_meta":{"origin":1086,"position":3},"title":"Getting real client IP address in PHP","author":"Virendra Chandak","date":"October 23, 2011","format":false,"excerpt":"Many times we need the visitor's ipaddress for validation, security, spam prevention, etc. Getting the Visitor's ipaddress is very easy in PHP. The simplest way to get the visitor's\/client's ipaddress is using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables. The variable in the $_SERVER array are created by the web server (like\u2026","rel":"","context":"In &quot;Functionality&quot;","block_context":{"text":"Functionality","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/functionality\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1673,"url":"https:\/\/www.virendrachandak.com\/techtalk\/using-php-create-passwords-for-htpasswd-file\/","url_meta":{"origin":1086,"position":4},"title":"How to generate passwords for .htpasswd using PHP","author":"Virendra Chandak","date":"March 2, 2014","format":false,"excerpt":"In my earlier post about .htaccess I had described about authentication using .htaccess and command to generate .htpasswd file. However, when we want to add passwords for many users that method will take too long, since we will have to add passwords for each user one at a time. However,\u2026","rel":"","context":"In &quot;Security&quot;","block_context":{"text":"Security","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/security\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2196,"url":"https:\/\/www.virendrachandak.com\/techtalk\/using-php-bcrypt-algorithm-for-htpasswd-generation\/","url_meta":{"origin":1086,"position":5},"title":"Generating bcrypt .htpasswd passwords using PHP","author":"Virendra Chandak","date":"September 23, 2019","format":false,"excerpt":"In my previous post we saw how to generate .htpasswd file using crypt and apr1-md5 algorithm in PHP. However, now there is a more secure BCRYPT algorithm that can be used since apache 2.4 for passwords in .htpasswd. In this post we will generate .htpasswd file using the BCRYPT algorithm\u2026","rel":"","context":"In &quot;Security&quot;","block_context":{"text":"Security","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/security\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/comments?post=1086"}],"version-history":[{"count":0,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1086\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/media?parent=1086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/categories?post=1086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/tags?post=1086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}