What are the potential pitfalls of sorting arrays based on one criteria and retrieving related values in PHP?

When sorting arrays based on one criteria and retrieving related values in PHP, a potential pitfall is that the index of the sorted array may not match the original array, leading to incorrect retrieval of related values. To solve this issue, you can use array_multisort() function to sort multiple arrays simultaneously while maintaining the relationship between the elements.

// Original array with related values
$names = array("John", "Alice", "Bob");
$ages = array(25, 30, 28);

// Sort the names array and maintain the relationship with ages array
array_multisort($names, $ages);

// Retrieve related values based on sorted names
for ($i = 0; $i < count($names); $i++) {
    echo $names[$i] . " - " . $ages[$i] . PHP_EOL;
}