3

Doing print_r on my array produces:

Array
(
    [0] => dogs
    [1] => cats

    [2] => birds
)

The newline between cats and birds is causing a problem. I did the following and the spacing still persists: array_walk($arr,'trim');

What can be done to remove this spacing?

8
  • 2
    How are the entries being populated? Commented Mar 10, 2014 at 12:30
  • how you populate the array? Commented Mar 10, 2014 at 12:30
  • Can you var_export($yourarray); and post it here ? Commented Mar 10, 2014 at 12:30
  • 2
    Most probably you have forgotten a new line in the cats entry. Commented Mar 10, 2014 at 12:31
  • The final array element is populated by $arr[] = "birds" Commented Mar 10, 2014 at 12:31

2 Answers 2

9

array_walk returns a boolean. Use array_map instead:

$arr = array_map("trim", $arr);
Sign up to request clarification or add additional context in comments.

Comments

8

array_walk won't help you, since it does not by itself persist any changes to the data. Use array_map instead:

$arr = array_map('trim', $arr);

If possible you should eliminate that extraneous line break from the beginning though, not filter it out after the fact.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.