How does PHP sort two-dimensional arrays?

Source: Internet
Author: User
How does PHP sort two-dimensional arrays? Two-dimensional arrays are often used in PHP development, but their sorting is not as convenient as using built-in functions as one-dimensional arrays, (For details about one-dimensional array sorting, refer to another article on this site [detailed summary of array sorting functions in PHP ]). We need to write a function to sort two-dimensional arrays. here, UncleToo will share with you a PHP two-dimensional array sorting function: Code: How does func PHP implement two-dimensional array sorting?

Two-dimensional array inPHPIt is often encountered during development, but its sorting is not as convenient as using built-in functions as a one-dimensional array, (For details about one-dimensional array sorting, refer to another article on this site [detailed summary of array sorting functions in PHP ]). We need to write a function to sort two-dimensional arrays.UncleTooSharePHPTwo-dimensional array sorting function:

Code:

functionarray_sort($arr,$keys,$type='asc'){$keysvalue= $new_array= array();foreach($arras$k=>$v){    $keysvalue[$k] = $v[$keys];}if($type== 'asc'){    asort($keysvalue);}else{    arsort($keysvalue);}reset($keysvalue);foreach($keysvalueas$k=>$v){    $new_array[$k] = $arr[$k];}return$new_array;}

?

Three parameters of the function are described as follows:

$ Arr: array to be sorted

$ Keys: specifies the key value to sort.

$ Type: sorting method. the default value is ascending or descending?

ThisPHPThe function sorts a two-dimensional array by the specified key value and returns the sorted array.

Call example:

?

$newArray= array_sort($array,'price');

?

Source: http://www.uncletoo.com/html/base/657.html

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.