PHP multi-dimensional array sort array detailed

Source: Internet
Author: User
Tags array sort
This article explains the PHP multidimensional array sorting array.

Array of PHP arrays sorted by field

/** * Sort array by filed and type, Common utility method. * @param array $data * @par Am String $sort _filed* @param string $sort _type sort_asc or Sort_desc */public function Sortbyonefield ($data, $filed, $typ e) {if (count ($data) <= 0) {return $data;} foreach ($data as $key = = $value) {$temp [$key] = $value [$filed];} arr Ay_multisort ($temp, $type, $data); return $data; The array of PHP arrays is sorted by two dimensions, sorted by first field, sorted by second field * Sort array by filed and type, common utility method. * @param array $array * @param string $filed 1 * @param string $type 1 sort_asc or Sort_desc * @param string $filed 2 * @para M string $type 2 Sort_asc or SORT_DESC */Public Function sortbytwofiled ($data, $filed 1, $type 1, $filed 2, $type 2) {if (cou NT ($DATA) <= 0) {return $data;} foreach ($data as $key = = $value) {$temp _array1[$key] = $value [$filed 1]; $temp _ar ray2[$key] = $value [$filed 2]; } array_multisort ($temp _array1, $type 1, $temp _array2, $type 2, $data); return $users; }

Sortmultiarray () supports up to 3-dimensional array ordering, of course, the custom method overload implements the sort of multidimensional numbers, where multidimensional is said to be multiple fields of data.

How to use:

1. Sortmultiarray ($data, [' score ' = Sort_desc]) 2. Sortmultiarray ($data, [' score ' = Sort_desc, ' count ' = Sort_asc]) 3. Sortmultiarray ($data, [' score ' = = Sort_desc, ' count ' = = sort_asc, ' name ' = = Sort_asc])/** * SORT multi array by F Iled and type. * @param data $array * @param condition $array */Public Function Sortmultiarray (& $data, $condition) {if (count ($data ) <= 0 | | Empty ($condition)) {return $data;} $dimension = count ($condition); $fileds = Array_keys ($condition); $types = Array_values ($condition); Switch ($dimension) {Case 1: $data = $this->sort1dimension ($data, $fileds [0], $types [0]); Case 2: $data = $this ->sort2dimension ($data, $fileds [0], $types [0], $fileds [1], $types [1]); Break Default: $data = $this->sort3dimension ($data, $fileds [0], $types [0], $fileds [1], $types [1], $fileds [2], $types [2]); Break } return $data; The Public Function sort1dimension (& $data, $filed, $type) {if (count ($data) <= 0) {return $data;} foreach ($Data as $key = = $value) {$temp [$key] = $value [$filed];} array_multisort ($temp, $type, $data); return $data;  } Public Function Sort2dimension (& $data, $filed 1, $type 1, $filed 2, $type 2) {if (count ($data) <= 0) {return $data; } foreach ($data as $key = + $value) {$sort _filed1[$key] = $value [$filed 1]; $sort _filed2[$key] = $value [$filed 2];} AR Ray_multisort ($sort _filed1, $type 1, $sort _filed2, $type 2, $data); return $data; } Public Function Sort3dimension (& $data, $filed 1, $type 1, $filed 2, $type 2, $filed 3, $type 3) {if (count ($data) <= 0 {return $data;} foreach ($data as $key = + $value) {$sort _filed1[$key] = $value [$filed 1]; $sort _filed2[$key] = $val ue[$filed 2]; $sort _filed3[$key] = $value [$filed 3]; } array_multisort ($sort _filed1, $type 1, $sort _filed2, $type 2, $sort _filed3, $type 3, $data); return $data; }

This article explains the PHP multi-dimensional array sorting array, more relevant content please follow the PHP Chinese web.

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.