I have the following array.
$state = array("gujarat","andhra_pradesh","madhya_pradesh","uttar_pradesh");
Expected Output
$state = array("Gujarat","Andhra Pradesh","Madhya Pradesh","Uttar Pradesh");
I want to convert array values with each first character of a word with UpperCase
and replace _ with space. So I do it using this loop and it working as expected.
foreach($states as &$state)
{
$state = str_replace("_"," ",$state);
$state = ucwords($state);
}
But my question is: is there any PHP function to convert the whole array as per my requirement?
You can use the array_map
function.
function modify($str) {
return ucwords(str_replace("_", " ", $str));
}
Then in just use the above function as follows:
$states=array_map("modify", $old_states)
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