PHP development skills (2)-Case Study of sorting two-dimensional arrays by key

Source: Internet
Author: User
In PHP, many built-in functions are used to process arrays. in many cases, we can directly use their built-in functions to meet our needs and obtain the expected results. However, sometimes, we cannot implement our requirements by using built-in functions. Therefore, we need to write algorithms to implement our own ideas. The following describes how to sort two-dimensional arrays by keys. In PHP, many built-in functions are used to process arrays. in many cases, we can directly use their built-in functions to meet our needs and obtain the expected results. However, sometimes, we cannot implement our requirements by using built-in functions. Therefore, we need to write algorithms to implement our own ideas. The following describes how to sort two-dimensional arrays by keys.

Implementation method:

 $ V) {$ key_value [$ k] = $ v [$ keys];} if ($ type = 'asc ') {asort ($ key_value );} else {arsort ($ key_value);} reset ($ key_value); foreach ($ key_value as $ k => $ v) {$ new_array [$ k] = $ arr [$ k];} return $ new_array;} $ arr = array ('name' => 'A ', 'Sex '=> 'm', 'sort' => 5), array ('name' => 'C', 'Sex '=>'m ', 'sort '=> 8), array ('name' => 'G', 'sex' => 'm', 'sort' => 3 ), array ('name' => 'e ', 'Sex '=> 'W', 'sort' => 6), array ('name' => 'B', 'Sex '=> 'W ', 'sort '=> 2),); // print the original array print_r ($ arr); // Print the sorted array print_r (array_sort ($ arr, 'name');?>

The output result shows that the array is sorted by "name.

// Original Array ([0] => Array ([name] => a [sex] => m [sort] => 5) [1] => Array ([name] => c [sex] => m [sort] => 8) [2] => Array ([name] => g [sex] => m [sort] => 3) [3] => Array ([name] => e [sex] => w [sort] => 6) [4] => Array ([name] => B [sex] => w [sort] => 2 )) // sorted Array ([2] => Array ([name] => g [sex] => m [sort] => 3) [3] => Array ([name] => e [sex] => w [sort] => 6) [1] => Array ([name] => c [sex] => m [sort] => 8) [4] => Array ([name] => B [sex] => w [sort] => 2) [0] => Array ([name] => a [sex] => m [sort] => 5 ))

The above is the PHP development technique (2)-details about how to sort two-dimensional arrays by key. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.