I have problem making answering this question
<?php
$chars = ["A", 1, 2, "h", "m", "E", "D"];
//Needed Output is: Ahmed
I tried to make that
foreach ($chars as $char) {
if (gettype($char) == "string") {
echo strtolower($char);
}
}
The Output is:
ahmed
but I don't how to make the first letter Capital, Is there any function can do that with arrays?
CodePudding user response:
Short answer there is ucfirst , but it only works on string not arrays
So we need to prepare a little bit. You can use implode to convert the array and glue it with empty seperator.
ucfirst with convert only the first letter to Capital letter, so we need to make shure that other letters small letter we can use strtolower to do that
And finaly I prefer using array_filter for that foreach in you code
function testChar($char) {
return gettype($char) === "string";
}
$chars = ["A", 1, 2, "h", "m", "E", "D"];
$result = array_filter($chars, "testChar");
$stringResult = implode("", $result);
$toLowerstringResult = strtolower($stringResult);
$ucfirstResult = ucfirst($toLowerstringResult);
echo $ucfirstResult;