PHP two-dimensional array ordering ____php

Source: Internet
Author: User
Tags function definition
There are two methods in which the first method returns an array that, if reordered and Ajaxreturn, becomes a jsonobject instead of a jsonarray. The second method is array.
1.PHP two-dimensional array sorting function The order of a one-dimensional array of PHP can be in sort (), Asort (), Arsort (), but the sort of PHP two-dimensional array needs to be customized.
The following function is to sort a given two-dimensional array by the specified key value, first look at the function definition:
function Array_sort ($arr, $keys, $type = ' asc ') {$keysvalue = $new _array = Array (); foreach ($arr as $k => $v) {$keysvalue [$ K] = $v [$keys]; } if ($type = = ' asc ') {asort ($keysvalue);} else{Arsort ($keysvalue);} reset ($keysvalue);  foreach ($keysvalue as $k => $v) {$new _array[$k] = $arr [$k];} return $new _array; }

It can sort a two-dimensional array by the specified key value, or you can specify ascending or descending sort (default is ascending), using the example:

$array = Array (' name ' => ' cell phone ', ' brand ' => ' Nokia ', ' Price ' =>1050), Array (' name ' => ' laptop ', ' brand ' => ') Lenovo ', ' Price ' =>4300, Array (' name ' => ' Razor ', ' Brand ' => ' Philips ', ' Price ' =>3100), Array (' name ' => ' treadmill ', ' Brand ' => ' three and pine rock ', ' Price ' =>4900, Array (' name ' => ' watch ', ' brand ' => ' Casio ', ' Price ' =>960), Array (' name ' => ' LCD tv ', ' Brand ' => ' Sony ', ' Price ' =>6299, Array (' name ' => ' laser printer ', ' Brand ' => ' hp ', ' Price ' =>1200));
$ShoppingList = Array_sort ($array, ' price '); Print_r ($ShoppingList);
The above is a sort of $array this two-dimensional array in terms of ' price ' from low to high.
Output result: (slightly).


The application of 2.PHP two-dimensional array in sorting
Recently developed a two-time API interface, have been looking for PHP can be directly to the two-dimensional array of a key to sort, usually php for one-dimensional array sorting simpler, but when we read the database to write an array, it may be a series of complex operations, the final form of a two-dimensional array, That's if you want to browse the data better, you have to sort the two-dimensional array by different key names.
Check out the manual PHP itself has a multidimensional array of functions to sort:
BOOL Array_multisort (array $arr 1 [, Mixed $arg [, mixed $ ... [, Array $ ...]]] )
The following is a description of the Array_multisort function in the manual:
Array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional. The association (string) key name remains unchanged, but the numeric key name is indexed. The input array is treated as a column of a table and sorted in rows--this is similar to the function of the SQL ORDER BY clause. The first array is the primary array to sort. The rows (values) in the array are the same, sorted by the size of the corresponding values in the next input array, and so on.

Since we usually take out the processed two-dimensional array is this:
$arr = Array (1 => array (' ID ' => 2, ' FID ' => ' 3 ', ' Data ' => ' AA '), 2 => array (' ID ' => 3, ' FID ' =& Gt ' 1 ', ' Data ' => ' BB '), 3 => array (' ID ' => 1, ' FID ' => ' 2 ', ' Data ' => ' BB ')); If we need to sort the array according to the FID, we're going to take the FID out of this column as required by the Array_multisort function, and then adjust the original array in ascending order, depending on the order of the columns.
function Multi_array_sort ($multi _array, $sort _field, $sort _type = sort_asc) {if (!is_array ($multi _array)) return FALSE;     foreach ($multi _array as $row) {if (! Is_array ($row)) return FALSE;   $arr _field[] = $row [$sort _field];   Array_multisort ($arr _field, $sort _type, $multi _array); return $multi _array; }//look at the results Print_r (Multi_array_sort ($arr, ' fid ', sort_desc));



PHP gets all the values of a particular key (array subscript) in a one-dimensional or multidimensional array

/*
author:yangyu@sina.cn
Description: Take out all the values of a one-dimensional or multidimensional array based on a particular key (subscript); The reason for not looping is to consider the efficiency of large arrays, serialize the arrays, and then extract the required strings according to the characteristics of the serialized structure.
*/
function Array_get_by_key (array $array, $string) {
if (!trim ($string)) return false;
Preg_match_all ("/\" $string \ "; \w{1}:(?: \ d+:|) (. *?);/", Serialize ($array), $res);
return $res [1];
}


$r = array (' ID ' => 1, ' s ' =>, ' a ' => array (' s ' => 123, Array (1, 2, ' s ' => ' ASDASDGSADGGSADG '));
Echo ' <pre> ';
Print_r (Array_get_by_key ($r, ' s '));
/*
Result
Array
(
[0] => 23
[1] => 123
[2] => "ASDASDGSADGGSADG"
)
*/


Original address:

http://hi.baidu.com/zwy654350304/item/6cc44bd41dcfa03948e1dd0a

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.