How PHP uses a key value to sort two-dimensional arrays

Source: Internet
Author: User

The ordering of a two-dimensional array in PHP can be implemented in several ways, such as a two-dimensional array below that is sorted by the ' age ' key value.

Two-dimensional arrays:

$arr =[    Array (                ' name ' = ' A ',                ' age ' =>28    ),    Array (            ' name ' = ' = ' B ',            ' age ' =>14    ), Array (            ' name ' = = ' C ',            ' age ' =>59    ),    Array (            ' name ' = = ' D ', ' Age            ' =>23    ),    Array (            ' name ' = ' E ',            ' age ' =>23    ),    Array (            ' name ' = ' = ' F ',            ' Age ' =>21    ),];

Sort by ' age ' key value:

Array_multisort (Array_column ($arr, ' age '), Sort_desc, $arr);p rintr ($arr);

where Array_column (an array, a key value in an array) is a single-dimensional array that represents a column that takes a key value out of a multidimensional array;

Array_multisort (Array (one-dimensional arrays), sort (SOTR_ASC,SOTR_DESC), other arrays (which can be two-dimensional) can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.

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.