Unexpected NULL Values When Using `array_map` with Closures in PHP 8.2
I need some guidance on I'm having a hard time understanding I'm working with an scenario with `array_map` when using closures in PHP 8.2... I have an array of user data, and I'm trying to append a formatted string to each user's name by using a closure within `array_map`. However, it seems that for some users, the result is unexpectedly `NULL`. Here’s the code I've written: ```php $users = [ ['name' => 'John'], ['name' => 'Jane'], ['name' => null], // This user has a null name ['name' => 'Doe'] ]; $formattedNames = array_map(function($user) { return strtoupper($user['name']) . ' (USER)'; }, $users); print_r($formattedNames); ``` When I run this code, I get: ``` Array ( [0] => JOHN (USER) [1] => JANE (USER) [2] => (USER) [3] => DOE (USER) ) ``` The second user with a null name is resulting in a formatted name of just `(USER)`. I wanted to replace `null` names with a default string like 'Unknown User'. I tried adding a check inside the closure, but it didn’t seem to solve the question: ```php $formattedNames = array_map(function($user) { return strtoupper($user['name'] ?? 'Unknown User') . ' (USER)'; }, $users); ``` This still produces the same output for the user with a null name. I also tried using `array_filter` to exclude users with a null name before mapping, but I still want to maintain the structure of the array. What’s the best way to handle this scenario while ensuring I don’t lose any user entries? Am I missing something in my approach or is there a more optimal way to handle this in PHP 8.2? Any help would be appreciated. Any pointers in the right direction? This is for a web app running on Ubuntu 20.04. Any advice would be much appreciated. What's the best practice here?