Php method for sorting arrays based on the size of a key value

Source: Internet
Author: User
This time for everyone to bring PHP according to the size of a key value array method, PHP according to the size of a key value of the array of considerations, the following is the actual case, together to see.

problem: sorting key values for a key of a given array

Workaround:

$a is a sorted array, $b is a collection of data to sort, $result is the final result $b = Array (' name ' = ' = '  Beijing ', ' nums ' = ' + '),  Array (' name ' = = ' Shanghai ', ' nums ', ' + ', '  array ' (' name ' = ' + ' guangzhou ', ' nums ' + ') '),  Array (' name ' = ' = ' shenzhen ', ' nums ' = ' 70 ')  ), $a = Array (), foreach ($b as $key = = $val) {  $a [] = $val [' nums '];//here to note $val[' nums '] cannot be empty, otherwise there will be a problem}//$ A first sorts rsort ($a), $a = array_flip ($a), $result = Array (), and foreach ($b as $k + = $v) {  $temp 1 = $v [' nums '];  $temp 2 = $a [$temp 1];  $result [$temp 2] = $v;} Here also to sort $result, Jian's position is not ksort ($result);//Then you want to see the results of Var_dump ($result);

Operation Result:

Array (4) {[0]=> Array (2) {  ["name"]=>  string (4) "Beijing"  ["Nums"]=>  string (3) "$"} [1]=> Array (2) {  ["name"]=>  string (4) "Guangzhou"  ["Nums"]=>  string (3) ""} [2]=> Array (2) {  [" Name "]=>  string (4)" Shanghai "  [" Nums "]=>  string (2)" a "} [3]=> Array (2) {  [" name "]=>  String (4) "Shenzhen"  ["Nums"]=>  string (2) "70"}}

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Implementation request refund steps in detail

Php namespace namespace definition and import use case analysis

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.