How can I trim all strings in an Array?

You can use the array_map() function to trim all strings in an array in PHP. The array_map() function applies a callback function to each element of an array and returns an array containing the results. In this case, the callback function is the trim() function, which trims whitespace from the beginning and end of a string.

Here's an example of how to trim all strings in an array:

<?php

$array = ["  first  ", " second ", "  third "];

// Trim all elements in the array using the array_map() function
// The trim() function is the callback function applied to each element
$trimmed_array = array_map('trim', $array);

print_r($trimmed_array);

// Output:
// Array ( [0] => first [1] => second [2] => third )

Watch a course Learn object oriented PHP

In this example, the array_map() function applies the trim() function to each element in the $array variable and returns a new array $trimmed_array containing the trimmed strings. The print_r() function is used to display the contents of the $trimmed_array variable, which should show that all whitespace has been removed from the beginning and end of each string.