How can PHP sort functions be used to sort data from a text file?

To sort data from a text file using PHP sort functions, you can read the data from the file into an array, apply the desired sorting function, and then write the sorted data back to the file. This can be achieved by using functions like file_get_contents() to read the data, explode() to split the data into an array, sort() to sort the array, and file_put_contents() to write the sorted data back to the file.

// Read data from text file into an array
$data = file_get_contents('data.txt');
$dataArray = explode("\n", $data);

// Sort the array
sort($dataArray);

// Write sorted data back to the text file
$sortedData = implode("\n", $dataArray);
file_put_contents('sorted_data.txt', $sortedData);