如何删除数组值中的空格? [重复]

This question already has an answer here:

I'm iterating through an XML and get a specific node. This node will be saved into an array but there is a problem. The array values have a space like this:

array(4) { ["host"]=> string(55) " localhost " ["username"]=> string(50) " root " ["dbName"]=> string(56) " test " ["dbPass"]=> string(52) " 123456 " }

You can see each values have a space before and after the value. The final result that I want achieve is this:

array(4) { ["host"]=> string(55) "localhost" ["username"]=> string(50) "root" ["dbName"]=> string(56) "test" ["dbPass"]=> string(52) "123456" }

If I do trim($array_node) I get an empty array.

</div>

If $array_node is the array itself, rather than one of the values within it, then that would explain why you are getting back an empty array.

A quick solution to apply the 'trim' function to all the values in the array would be the following:

$result_array = array_map('trim', $source_array);

You may be able to do this when building the array, but you don't show that code. To do it after the fact, just trim:

$result = array_map('trim', $array);

You originally stated array index multiple times. This would do it for the index:

$result = array_combine(array_map('trim', array_keys($array)), $array);