I am using following code in php: Code:
$input = array(" text1","text2 "," text3 "," text4");
$output = array();
foreach($input as $val)
{
$output[] = trim($val);
}
var_dump($output);
Is it possible to trim array elements value without foreach loop?
You can use array_map
:
$output = array_map('trim', $input);
Of course this will still iterate over the array internally.
This should work just fine;
$input = array(" text1","text2 "," text3 "," text4");
$output = array_map('trim', $input);
var_dump($output);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With