Implementation of a PHP two-dimensional array sorted by the specified field

Source: Internet
Author: User
This article mainly introduces the PHP two-dimensional array according to the specified field of the implementation of the order, has a certain reference value, now share to everyone, the need for friends can refer to

Problem: After merging two arrays with PHP's Array_merge () function, you want to sort the new array by the ' post_time ' field in the two arrays

Workaround: By consulting the official manual, I learned that there is a function of Array_multisort () that can sort multiple arrays or multidimensional arrays, returning an array after sorting, where the string key name is preserved, but the number key name is re-indexed, starting at 0 and incrementing by 1.

This function is encapsulated below for easy invocation:

/** * A two-dimensional array is sorted by the specified field * @params array $array needs to be sorted * @params string $field sorted field * @params string $sort sort order Flag Sort_desc Descending; Sort_asc Ascending */function arraysequence ($array, $field, $sort = ' Sort_desc ') {    $arrSort = array ();    foreach ($array as $uniqid + $row) {        foreach ($row as $key + = $value) {            $arrSort [$key] [$uniqid] = $value; 
 }    }    array_multisort ($arrSort [$field], constant ($sort), $array);    return $array;}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.