This Post is About Removing White space of array Members from the beginning and end of the given array. Here is the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // names array with 4 members $names = array('arjun ',' naveen ','Raj ','Mahesh '); //array_map() — Applies the callback to the elements of the given arrays, stores the result in new array //Any built in PHP function can be mapped in this way, to affect all members of an array. $new_names = array_map('trim',$names); print_r($new_names); // output /*Array ( [0] => arjun [1] => naveen [2] => Raj [3] => Mahesh ) */ |
Another Method :
1 2 3 4 5 6 7 8 9 10 11 |
// names array $names = array('arjun ',' naveen ','Raj ','Mahesh '); // trim array values using this function "trim_filter" function trim_filter(&$names) { $names = trim($names); // this removes whitespace from the beginning and end of the name } array_filter($names, 'trim_filter'); //the data in $names is trimmed print_r($names); |
Used PHP functions
array_map() — Applies the callback to the elements of the given array, returns result in new array
trim() — Strip white-space (or other characters) from the beginning and end of a string
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
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.
Subscribe
Login
0 Comments