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
Tip:
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'=>'1111','date'=>'2222'),'second'=>array('title'=>'1111','date'=>'2222'),'third'=>array('title'=>'2222','date'=>'3333')); 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($a); /*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($a); /* 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($a); /* 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 2005/09/24 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"……) * @author Chunsheng Wang
* @param array $ArrayData the array to sort. * @param string $KeyName1 the first item to sort by. * @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 array sorted 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
Function rebuild_array ($ arr) {// rebuild a array static $ tmp = array (); for ($ I = 0; $ I
Instance:
Else '); // defines a three-dimensional array to detect our function echo''; print_r(rebuild_array($arr)); echo '
';
4. delete blank elements from the array
function array_remove_empty(&$arr, $trim = true) { foreach ($arr as $key => $value) { if (is_array($value)) { array_remove_empty($arr[$key]); } else { $value = trim($value); if ($value == '') { unset($arr[$key]); } elseif ($trim) { $arr[$key] = $value; } } } }
Instance:
$ A = array (3), 2, '', array ('', 23), 0); array_remove_empty ($ a); printr ($ ); // find the printr function in this article
5. obtain the value of a specific key under a multi-dimensional array and generate a one-dimensional array.
function getall_by_key(array $arr, $key){ if (!trim($key)) return false; preg_match_all("/\"$key\";\w{1}:(?:\d+:|)(.*?);/", serialize($arr), $output); return $output[1]; } $testArr = array('111'=> 100, 'out'=> 200, '333' => array('out' => 310, array(321, 322, 'out' => "this is a test"))); printr(getall_by_key($testArr, 'out'));
The above is all the content in this article. php introduces the corresponding operations on two-dimensional arrays in detail and hopes to help you learn.