/ Published in: PHP
Simple function that sorts a bi-dimensional array by one of it keys.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * sort_array_by_key() * * @param array $array * @param string|int $key * @return array * * @author Alejo */ function array_sort_by_key($array, $key) { foreach ($array as $ar_item) { $tmpArKeys[] = $ar_item[$key]; } foreach ($tmpArKeys as $n_key => $n_val) { $newAr[] = $array[$n_key]; } return $newAr; } /// Example 'Name' => 'John', 'Lastname' => 'Doe', ), 'Name' => 'Harry', 'Lastname' => 'Potter', ), 'Name' => 'Lady', 'Lastname' => 'Gaga', ) ); $sorted_people = array_sort_by_key($people, 'Name'); /// Example output: /* Array ( [0] => Array ( [Name] => Harry [Lastname] => Potter [E-mail] => [email protected] ) [1] => Array ( [Name] => John [Lastname] => Doe [E-mail] => [email protected] ) [2] => Array ( [Name] => Lady [Lastname] => Gaga [E-mail] => [email protected] ) ) */