For array_walk
to modify the items (values) in the array, the callback must be a function that takes its first parameter by reference and modifies it (which is not the case of plain trim
), so your code would become:
$a=array('test_data_1 ','test_data_2');
array_walk($a, function (&$value) { $value = trim($value); }); // by-reference modification
// (no array_map)
foreach($a AS $b){
var_dump($b);
}
Alternatively, with array_map
you must reassign the array with the return value, so your code would become:
$a=array('test_data_1 ','test_data_2');
// (no array_walk)
$a = array_map('trim', $a); // array reassignment
foreach($a AS $b){
var_dump($b);
}