array_map不提供空数组

I have the below line which takes a string, explodes it to an array by ',' and then trims any whitespace for each array item.

   $essentialArray = array_map('trim', explode(',', $essential_skills));

However when $essential_skills string = "" $essentialArray will equal Array ( [0] => )

But I need it to equal Array() so that I can call empty() on on it.

explode return Array ( [0] => ), so you need to check your string before array_map

here is a one solution

   $exploderesult = $essential_skills ? explode(',', $essential_skills) : array();
   $essentialArray = array_map('trim', $exploderesult );

That is normal and logical behavior of the function.

Look at it this way: what does the explode() call return? An array with a single element which is the empty string. The array_map() call does not change that at all.

So you might ask: why does explode('') result in an array with a single element which is the empty string? Well, why not? It takes the empty string and splits it at all comma characters in there, so exactly no times. That means the empty string stays unaltered. But it does not magically vanish!

The easiest solution here will be just filter result array, like this:

$essentialArray = array_filter(array_map('trim', explode(',', $essential_skills)));

but proper way is:

if ($essential_skills === '') {
$essentialArray = [];
}
else {
$essentialArray = array_map('trim', explode(',', $essential_skills));
}