Tip tips:
Array_keys ($array)//Return all key names
array_values ($array)//Return all key values
$result =array_reverse ($input);//invert array without retaining key name
$result _keyed=array_reverse ($input, true);//reverse array, keep key name
Array_keys ($array, "blue");//Return key name with blue value
1. PHP two-dimensional array to repeat the function
PHP array Remove Duplicates there is a built-in function Array_unique (), but the PHP Array_unique function only applies to one-dimensional arrays, which do not apply to multidimensional arrays, the following provides a two-dimensional array of array_unique functions
function Unique_arr ($array 2d, $stkeep =false, $ndformat =true) {//To determine whether to retain the first-level array key (one-level array key can be thought
Non-numeric) if ($stkeep) $STARR = Array_keys ($array 2D);
Determines whether to keep level two array keys (All level two array keys must be the same) if ($ndformat) $NDARR = Array_keys (end ($array 2D));
Dimension reduction, you can also use implode to convert a one-dimensional array to a comma-connected string of foreach ($array 2D as $v) {$v = join (",", $v);
$temp [] = $v;
//Remove the duplicated string, which is the repeating one-dimensional array $temp = Array_unique ($temp);
The disassembled array is then reassembled with the 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; }
$array 2D = Array (' =>array ' => ' 1111 ', ' Date ' => ' 2222 '), ' second ' =>array (' title ' => ' 1111 ', ' Date ' => ' 2222 '), ' third ' =>array (' title ' => ' 2222 ', ' Date ' => ' 3333 '));
Print_r ($array 2D);
Print_r (Unique_arr ($array 2d,true));
2. Two-D array sorting method
One-dimensional array sorting method:
Common functions:
function Printr ($arr)
{
echo ' <pre> ';
Print_r ($arr);
Echo ' </pre><br> ';
}
The First group: sort and Rsort , sorted according to the order of the PHP array key values, ASC and Reverse DESC, and destroy the index relationship of the original array--actually, after the index is deleted, the index of the number starting from 0 is reset.
$a = Array (' a ' =>1,2);
Sort ($a); PRINTR ($a);
Rsort ($a); PRINTR ($a);
/*array
(
[0] => 1
[1] => 2
)
Array
(
[0] => 2
[1] => 1
)
The second set of functions: Asort and Arsort, These two functions are more powerful, as long as they can retain the original index relationship of the array, the example of the sort and rsort respectively with these two functions to replace the
$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 set of PHP array sort functions: Krsort and Ksort, these two different from the above two groups, the two functions are to sort the 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
)
*/
Using custom functions to sort the PHP array, there are three functions:
Uasort uses custom functions to sort the key values of the PHP array and retains the original index relationship.
Uksort uses custom functions to sort the key names of the PHP array and retains the original index relationship.
Usort the key values of the PHP array through a custom function, deletes the original index relationship, and creates a new index from scratch.
The following are two-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 the items use Array_multisort () function.
* * Sortarr ($Array, "Key1", "Sort_asc", "Sort_retular", "Key2" ...)
* @author Chunsheng Wang <wwccss@263.net> * @param array $ArrayData the array to sort.
* @param string $KeyName 1 The ' the ' the ' the '. * @param string $SortOrder 1 The order to sort by ("SORT_ASC" |) Sort_desc ") * @param string $SortType 1 the Sort type (" Sort_regular "| Sort_numeric "|"
Sort_string ") * @return array sorted array. */function Sortarr ($ArrayData, $KeyName 1, $SortOrder 1 = "Sort_asc", $SortType 1 = "Sort_regular") {if!is_array ($ArrayD
ATA)) 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:
################# example ################# $arr = Array (' name ' => ' learning ', ' size ' => ' 1235 ', ' Type ' => ' jpe ', ' Time ' => ' 1921-11-13 ', ' class ' => ' DD ',), Array (' name ' =&G T ' Chinese kung Fu ', ' size ' => ' 153 ', ' type ' => ' jpe ', ' Time ' => ' 2005-11-13 ', ' class ' => ' JJ ',), Array (' Name ' => ' programming ', ' Size ' => ', ' type ' => ' gif ', ' time ' => ' 1997-11-13 ', ' class ' => ' DD ',), Array (' name ' => ' Chinese Kung Fu ', ' size ' => ', ' type ') => ' jpe ', ' Time ' => ' 1925-02-13 ', ' class ' => ' yy ',), Array (' name ' => ' Chinese Kung Fu ')
, ' Size ' => ' 5 ', ' type ' => ' icon ', ' time ' => ' 1967-12-13 ', ' class ' => ' RR ',
),
);
Echo ' <pre> '; Print_r ($arr);
Echo ' <br> '; Note: 153:65 Small $temp = Sortarr ($arr, "name", "Sort_asc", "type", "when sorted numerically", "Sort_desc "," size "," Sort_asc "," sort_string ");
Print_r ($temp);
Echo ' </pre> ';
3. Multidimensional array to a one-dimensional array
function Rebuild_array ($arr) {//rebuild A array
static $tmp =array ();
For ($i =0 $i <count ($arr); $i + +) {
if (Is_array ($arr [$i])) {
Rebuild_array ($arr [$i]);
} else{
$tmp []= $arr [$i];
}
}
return $tmp;
}
Instance:
$arr =array (' 123.html ', ' 456.html ', Array (' dw.html ', ' fl.html ', Array (' ps.html ', ' fw.html ')), ' ab.html ');
Define a three-dimensional array to detect our function
echo ' <pre> ';
Print_r (Rebuild_array ($arr));
Echo ' </pre> ';
4. Remove the blank element from the array
Function Array_remove_empty (& $arr, $trim = True)
{
foreach ($arr as $key => $value) {
if Is_array ($va Lue)) {
array_remove_empty ($arr [$key]);
} else {
$value = Trim ($value)
; if ($value = = ") {
unset ($arr [$key]);
} elseif ($trim) {
$arr [$key] = $value;
}
}}
Instance:
$a = Array (Array (3), 2, ', ', Array (', '), 0);
Array_remove_empty ($a);
PRINTR ($a); Printr function Please find in this article
5. Get the value under a specific key in a multidimensional 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 (' => ', ' Out ' =>, ' 333 ' => Array (' Out ' => 310, Array (321, 322, ' out ' => ') Test "));
The above is the entire content of this article, PHP for two-dimensional array of the corresponding operation of the detailed introduction, I hope that the study of everyone to help.