PHP array_map for multidimensional arrays

Array map is one of the interesting PHP’s array function. But it will not work with multidimensional arrays as expected.So in this post, I would like to share a simple alternative technique for array_map(). Our function will work like array_map but it will support multidimensional arrays.

Here we used recursive function methodology and the Ternary Operator syntax.

How to use

you can use my_array_map just like as array_map().

That’s it.

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

2 replies on “PHP array_map for multidimensional arrays”


//$value = array_map(‘utf8_encode’, $value);

function my_array_map($funcion, $value)
$result = array();
foreach ($value as $key => $val)
$result[$key] = (is_array($val) ? my_array_map($function, $val) : $function($val));
return $result;


echo json_encode($value);

this shows $function is undefined

Leave a Reply

Your email address will not be published. Required fields are marked *