How can data in PHP arrays be compared and manipulated based on specific fields?

To compare and manipulate data in PHP arrays based on specific fields, you can use array functions such as array_filter(), array_map(), and usort(). These functions allow you to filter, transform, and sort arrays based on specific criteria. By using callback functions with these array functions, you can target specific fields within the arrays for comparison and manipulation.

// Sample array data
$data = [
    ['name' => 'John', 'age' => 30],
    ['name' => 'Jane', 'age' => 25],
    ['name' => 'Alice', 'age' => 35]
];

// Example: Sorting the array based on 'age' field
usort($data, function($a, $b) {
    return $a['age'] <=> $b['age'];
});

// Example: Filtering the array to get only users with age greater than 25
$filteredData = array_filter($data, function($item) {
    return $item['age'] > 25;
});

// Example: Mapping the array to transform 'name' field to uppercase
$transformedData = array_map(function($item) {
    $item['name'] = strtoupper($item['name']);
    return $item;
}, $data);