Konstruktionsplan
2 months ago

Fun with Maps! 🗺

Posted 2 months ago by Konstruktionsplan

Hello!

Code:

$arr = [1, 2, 3, 6, "7"];

function upwardTrend($arr) {
	 return array_map(function($item){
    	if(is_string($item)) {
        	return "Strings not permitted!";
        }
       return $item;
    }, $arr);
}

print_r(upwardTrend($arr));

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 6
    [4] => Strings not permitted!
)

I wanted to ask if this is "the best way" to check an array, for example, to see if it might contain strings where there should only be numbers.

In the past I used imm "foreach", but now I try to use "array_map" more often.

Is this a good style?

Thanks! ✨

Please sign in or create an account to participate in this conversation.