Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Trim Values in an Array or Multidimensional Array

This is a handy reference for trimming values in an associative array. While there's any number of ways to achieve the result, this works nicely.

1
<?php 
2
$results = array_map(array_map('trim', $results));

A multidimensional array is immune to the above code. The following function will work on a string or array.

1
<?php 
2
/*
3
 Trim Values in an Array or Multidimensional Array
4
 http://www.beliefmedia.com/code/php-snippets/trim-array-values
5
*/
6
 
7
function beliefmedia_trim($data) {
8
 if ($data == null) return null;
9
 
10
 if (is_array($data)) {
11
   return array_map('beliefmedia_trim_array', $data);
12
 } else return trim($data);
13
}

Got an easier way? Let us know.

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment