How can you manipulate and format specific elements within an array in PHP, such as changing date formats or concatenating strings?

To manipulate and format specific elements within an array in PHP, such as changing date formats or concatenating strings, you can loop through the array and apply the necessary formatting or manipulation functions to each element individually. This can be achieved using a foreach loop to iterate over the array elements and then applying the desired formatting or manipulation function to each element based on its type or key.

<?php

// Sample array with dates and strings
$data = [
    'date' => '2022-01-01',
    'name' => 'John Doe',
];

// Loop through the array and format specific elements
foreach ($data as $key => &$value) {
    if ($key === 'date') {
        $value = date('d/m/Y', strtotime($value)); // Change date format
    } elseif ($key === 'name') {
        $value = 'Hello, ' . $value; // Concatenate string
    }
}

print_r($data);

?>