What is the recommended approach for sorting a two-dimensional array in PHP?
When sorting a two-dimensional array in PHP, you can use the `array_multisort()` function to sort the rows based on a specific column. This function allows you to specify the column you want to sort by and the sorting order.
// Sample two-dimensional array
$array = array(
array("name" => "John", "age" => 30),
array("name" => "Jane", "age" => 25),
array("name" => "Alice", "age" => 35)
);
// Sort the array by the 'age' column in ascending order
array_multisort(array_column($array, 'age'), SORT_ASC, $array);
// Print the sorted array
print_r($array);
Keywords
Related Questions
- What are the differences between using mysql and mysqli functions in PHP, and how do they impact code compatibility?
- How can PHP be used to save the modified HTML template under a specific filename?
- What are some potential security risks in the provided PHP guestbook script and how can they be mitigated?