PHP implements multidimensional array sorting function sharing for specified fields

Source: Internet
Author: User
Tags array sort php code sort

This article mainly introduces the PHP implementation of the specified field of multidimensional array sorting function sharing, this article shares a section of the PHP specified field of multidimensional array sorting method code, this code can be implemented according to the field of the array of groups to sort, the need for friends can refer to the

PHP array sorting can be implemented in the Array_multisort method, but if it is a multidimensional array, and we want to specify a field in the array to sort, then we need to write our own method implementation. This article shares the code for a multidimensional array sort method for a PHP-specified field that can be sorted by the array of field fields.

The code is as follows:
Function Sortarrbyfield (& $array, $field, $desc = False) {
$FIELDARR = Array ();
foreach ($array as $k => $v) {
$FIELDARR [$k] = $v [$field];
}
$sort = $desc = = False? Sort_asc:sort_desc;
Array_multisort ($FIELDARR, $sort, $array);
}
The above is the PHP specified field of multidimensional array sorting method, I hope this section of PHP code to bring you help.

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.