5 May 2016

This snippet shows you how to trim an array with single line of code. This can be achieved by using "array_map".

Source code viewer
  1. $array = array(
  2. ' before and after ',
  3. ' before',
  4. 'after ',
  5. );
  6.  
  7. $array_map = array_map('trim', $array);
  8. print_r($array_map);
  9.  
  10. // Output:
  11. /*
  12. Array
  13. (
  14.   [0] => before and after
  15.   [1] => before
  16.   [2] => after
  17. )
  18. */
Programming Language: PHP