2

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.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Sandokan
  • 1,075
  • 3
  • 15
  • 30

2 Answers2

8

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);
Hiphop03199
  • 729
  • 3
  • 16
3

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);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87