# Posted By

AlejoLuc on 01/23/10

# Statistics

Viewed 495 times
Favorited by 1 user(s)

# Sort bidimensional array by one of it keys

/ Published in: PHP
Save to your folder(s)

Simple function that sorts a bi-dimensional array by one of it keys.

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){    \$tmpArKeys = array();    foreach (\$array as \$ar_item)    {        \$tmpArKeys[] = \$ar_item[\$key];    }    asort(\$tmpArKeys);     \$newAr = array();    foreach (\$tmpArKeys as \$n_key => \$n_val)    {        \$newAr[] = \$array[\$n_key];    }    return \$newAr;}  /// Example \$people = array(                array(                'Name' => 'John',                'Lastname' => 'Doe',                'E-mail' => '[email protected]'                ),                array(                'Name' => 'Harry',                'Lastname' => 'Potter',                'E-mail' => '[email protected]'                ),                array(                'Name' => 'Lady',                'Lastname' => 'Gaga',                'E-mail' => '[email protected]'                )                 ); \$sorted_people = array_sort_by_key(\$people, 'Name');print_r(\$sorted_people); /// 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]        ) ) */`