How can you add brackets around each element in an array in PHP?

To add brackets around each element in an array in PHP, you can use the array_map function along with a custom callback function that adds the brackets. The callback function should concatenate the element with brackets before and after it. This will create a new array with each element enclosed in brackets.

// Original array
$originalArray = ['apple', 'banana', 'orange'];

// Function to add brackets around each element
function addBrackets($item) {
    return '[' . $item . ']';
}

// Use array_map to apply the function to each element in the array
$newArray = array_map('addBrackets', $originalArray);

// Output the new array
print_r($newArray);