Php performs operations on two-dimensional arrays (sorting, conversion, and empty space removal)

Source: Internet
Author: User
This article describes how php performs operations on two-dimensional arrays, including sorting, transforming, removing spaces, and deduplication of two-dimensional arrays by php. For more information, see

This article describes how php performs operations on two-dimensional arrays, including sorting, transforming, removing spaces, and deduplication of two-dimensional arrays by php. For more information, see

Array_keys ($ array) // returns all key names. array_values ($ array) // returns all key values. $ result = array_reverse ($ input); // reverses the array, key name $ result_keyed = array_reverse ($ input, true) is not retained; // The array is reversed and the key name array_keys ($ array, "blue") is retained "); // The key name whose return value is blue

1. PHP two-dimensional array deduplication Function
PHP array deduplication has a built-in FunctionArray_unique ()However, php's array_unique function is only applicable to one-dimensional arrays and not to multidimensional arrays. The following provides a two-dimensional array's array_unique function.

Function unique_arr ($ array2D, $ stkeep = false, $ ndformat = true) {// determine whether to retain the first-level array key (the first-level array key can be non-numeric) if ($ stkeep) $ stArr = array_keys ($ array2D); // determines whether to retain the second-level array key (all second-level array keys must be the same) if ($ ndformat) $ ndArr = array_keys (end ($ array2D); // you can also use implode to convert a one-dimensional array to a comma-connected string foreach ($ array2D as $ v) {$ v = join (",", $ v); $ temp [] = $ v;} // remove duplicate strings, that is, the duplicate one-dimensional array $ temp = array_unique ($ temp); // re-assemble the split array foreach ($ temp as $ k => $ v) {if ($ stkeep) $ k = $ stArr [$ k]; if ($ ndformat) {$ tempArr = explode (",", $ v ); foreach ($ tempArr as $ ndkey => $ ndval) $ output [$ k] [$ ndArr [$ ndkey] = $ ndval ;} else $ output [$ k] = explode (",", $ v);} return $ output ;}

$ Array2D = array ('first' => array ('title' => '123', 'date' => '123 '), 'second' => array ('title' => '123', 'date' => '123 '), 'third' => array ('title' => '20140901', 'date' => '20140901'); print_r ($ array2D); print_r (unique_arr ($ array2D, true ));

2. Two-dimensional array sorting method
One-dimensional array sorting method:
Common functions:

Function printr ($ arr) {echo'

';print_r($arr);echo '

';}

Group 1: sort and rsortSort the keys of the PHP array by asc and desc in the reverse order, and break the index relationship of the original array. In fact, the index is deleted and then the number index starting from 0 is re-created.

$ A = array ('A' => 1, 2); sort ($ a); printr ($ a); rsort ($ a); printr ($ ); /* Array ([0] => 1 [1] => 2) Array ([0] => 2 [1] => 1 )*/

Second functions: asort and arsort,These two functions are a little more powerful, as long as they can retain the original index relationship of the array, replace the sort and rsort functions in the above example with the two functions respectively.

$ A = array ('A' => 1, 2); asort ($ a); printr ($ a); arsort ($ a); printr ($ ); /* Array ([a] => 1 [0] => 2) Array ([0] => 2 [a] => 1 )*/

The third group of PHP array sorting functions: krsort and ksort,These two functions are different from the above two groups, which sort key names.

$ A = array ('A' => 1, 2); ksort ($ a); printr ($ a); krsort ($ a); printr ($ ); /* Array ([0] => 2 [a] => 1) Array ([a] => 1 [0] => 2 )*/

Sort PHP arrays by using custom functions. The following three functions are available:
Uasort sorts the key values of the PHP Array Using a custom function and retains the original index relationship.
Uksort sorts the key names of PHP arrays by using a custom function and retains the original index relationship.
Usort sorts the key values of the PHP array by using a user-defined function, deletes the original index relationship, and creates a new index from scratch.
Below isTwo-dimensional sorting:

/*** @ Package BugFree * @ version $ Id: FunctionsMain. inc. php, v 1.32 11:38:37 wwccss Exp $ *** Sort an two-dimension array by some level two items use array_multisort () function. ** sortArr ($ Array, "Key1", "SORT_ASC", "SORT_RETULAR", "Key2 "......) * @ AuthorChunsheng Wang * @ Param array $ ArrayData the array to sort. * @ param string $ KeyName1 the first item to sort. * @ param string $ SortOrder1 the order to sort by ("SORT_ASC" | "SORT_DESC ") * @ param string $ SortType1 the sort type ("SORT_REGULAR" | "SORT_NUMERIC" | "SORT_STRING") * @ return arraysorted array. */function sortArr ($ ArrayData, $ KeyName1, $ SortOrder1 = "SORT_ASC", $ SortType1 = "SORT_REGULAR") {if (! Is_array ($ ArrayData) return $ ArrayData; // Get args number. $ ArgCount = func_num_args (); // Get keys to sort by and put them to SortRule array. for ($ I = 1; $ I <$ ArgCount; $ I ++) {$ Arg = func_get_arg ($ I); if (! Eregi ("SORT", $ Arg) {$ KeyNameList [] = $ Arg; $ SortRule [] = '$ '. $ Arg;} else $ SortRule [] = $ Arg;} // Get the values according to the keys and put them to array. foreach ($ ArrayData AS $ Key => $ Info) {foreach ($ KeyNameList AS $ KeyName) $ {$ KeyName} [$ Key] = strtolower ($ Info [$ KeyName]);} // Create the eval string and eval it. $ EvalString = 'array _ multisort ('. join (",", $ SortRule ). ', $ ArrayData);'; eval ($ EvalString); return $ ArrayData ;}

Instance:

// #####################################$ Arr = array ('name' => 'learn ', 'SIZE' => '123', 'type' => 'jpe', 'time' => '2017-11-13 ', 'class' => 'dd ',), array ('name' => 'Kung Fu China', 'SIZE' => '000000', 'type' => 'jpe ', 'time' => '2017-11-13 ', 'class' => 'JJ',), array ('name' => 'programmatically ', 'SIZE' => '35', 'type' => 'gif', 'time' => '2017-11-13 ', 'class' => 'dd ',), array ('name' => 'Kung Fu China', 'SIZE' => '65', 'type' => 'jpe ', 'time' => '2017-02-13 ', 'class' => 'yy',), array ('name' => 'Kung Fu of China ', 'SIZE' => '5', 'type' => 'icon ', 'time' => '2017-12-13 ', 'class' => 'rr ',),); echo'

'; Print_r ($ arr); echo'
'; // Note: When sorting by number, the ratio is 153 to 65. $ temp = sortArr ($ arr, "name", "SORT_ASC", "type", "SORT_DESC ", "size", "SORT_ASC", "SORT_STRING"); print_r ($ temp); echo'
';

3. Convert multi-dimensional arrays to one-dimensional arrays

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.