This example describes the PHP array action class. Share to everyone for your reference. as follows:
Class arrayhelper{/** * Remove whitespace elements from an array (including elements with only whitespace characters) * Usage: * @code php * $arr = Array (', ' test ', ');
* Arrayhelper::removeempty ($arr);
* * Dump ($arr);
*//The output will only be ' test ' * @endcode * * @param array $arr the arrays to be processed * @param boolean $trim whether to call the trim function on an array element * * static function Removeempty (& $arr, $trim = TRUE) {foreach ($arr as $key => $value) {if Is_arra
Y ($value)) {self::removeempty ($arr [$key]);
else {$value = Trim ($value);
if ($value = = ") {unset ($arr [$key]);
} elseif ($trim) {$arr [$key] = $value; /** * Returns all values of the specified key from a two-dimensional array * usage: * @code php * $rows = Array (* Array (' ID ' =>
1, ' value ' => ' 1-1 '), * array (' ID ' => 2, ' value ' => ' 2-1 '), *);
* $values = Arrayhelper::getcols ($rows, ' value ');
* * Dump ($values); *//output result is *//arrAY (*//' 1-1 ', *//' 2-1 ', *//) * @endcode * * @param array $arr Data source * @param string $col to query
The key * * * @return array contains the array of all values for the specified key/static function Getcols ($arr, $col) {$ret = array ();
foreach ($arr as $row) {if (Isset ($row [$col])) {$ret [] = $row [$col];
} return $ret; /** * Converts a two-dimensional array to HashMap and returns the result * Usage 1: * @code php * $rows = Array (* Array (' ID ' => 1, ' value ')
=> ' 1-1 '), * array (' ID ' => 2, ' value ' => ' 2-1 '), *);
* $hashmap = Arrayhelper::tohashmap ($rows, ' id ', ' value ');
* * Dump ($HASHMAP); *//output result is *//Array (*//1 => ' 1-1 ', *//2 => ' 2-1 ', *//) * @endcode * * If omitted $val
Uefield parameter, the result of the transformation is an array containing all the data for that item. * Usage 2: * @code php * $rows = Array (* Array (' ID ' => 1, ' value ' => ' 1-1 '), * array (' ID ' => 2,
' Value ' => ' 2-1 '), *); * $hashmap = Arrayhelper::tohashmap ($rows, ' ID');
* * Dump ($HASHMAP); *//output result is *//Array (*//1 => array (' ID ' => 1, ' value ' => ' 1-1 '), *//2 => Array (' ID ' => 2, ' value ' => ' 2-1 '), *//) * @endcode * * @param array $arr Data source * @param string $keyField Follow the value of what key to go Change * @param string $valueField corresponding key value * * @return array-converted HASHMAP-style arrays/static function Tohashmap ($arr, $
Keyfield, $valueField = NULL) {$ret = array ();
if ($valueField) {foreach ($arr as $row) {$ret [$row [$keyField]] = $row [$valueField];
} else {foreach ($arr as $row) {$ret [$row [$keyField]] = $row;
} return $ret; /** * Group A two-dimensional array by the value of the specified field * Usage: * @endcode * * @param array $arr Data source * @param string $keyField as
The result of grouping by key name * * @return array/static function GroupBy ($arr, $keyField) {$ret = array (); foreach ($arr as $row) {$key = $row [$keyField];
$ret [$key] = $row;
return $ret;
/** * Converts a planar two-dimensional array to a tree structure by the specified field * * * if you want to obtain a subtree of any node as root, you can use the $refs parameter: * @code php * $refs = null;
* $tree = Arrayhelper::tree ($rows, ' id ', ' parent ', ' nodes ', $refs);
* *//Output ID 3 node and all of its child nodes * $id = 3;
* Dump ($refs [$id]);
* @endcode * @param array $arr Data source * @param string $keyNodeId Node ID field name * @param string $keyParentId node parent ID field Name
* @param string $keyChildrens to save the field name of the child node * @param boolean $refs whether to include the node reference in the return result (* * Returns array tree)
static function Totree ($arr, $keyNodeId, $keyParentId = ' parent_id ', $keyChildrens = ' childrens ', & $refs = NULL) {
$refs = Array ();
foreach ($arr as $offset => $row) {$arr [$offset] [$keyChildrens] = array ();
$refs [$row [$keyNodeId]] =& $arr [$offset];
} $tree = Array ();
foreach ($arr as $offset => $row) {$parentId = $row [$keyParentId]; if ($parentId) {if (!isset ($refs [$parentId]))
{$tree [] =& $arr [$offset];
Continue
} $parent =& $refs [$parentId];
$parent [$keyChildrens] [] =& $arr [$offset];
else {$tree [] =& $arr [$offset];
} return $tree;
/** * Expands the tree array into a planar array * * This method is the reverse operation of the trees () method. * * @param array $tree tree array * @param string $keyChildrens The key name containing the child nodes * * @return array expanded arrays/static fun
Ction Treetoarray ($tree, $keyChildrens = ' childrens ') {$ret = array (); if (Isset ($tree [$keyChildrens]) && is_array ($tree [$keyChildrens])) {foreach ($tree [$keyChildrens] as $c
Hild) {$ret = Array_merge ($ret, Self::treetoarray ($child, $keyChildrens));
} unset ($node [$keyChildrens]);
$ret [] = $tree;
else {$ret [] = $tree;
return $ret; /** * To sort the array by the specified key pairs * * @endcode * * * @param arrays $array to order@param string $keyname sorted by the key * @param int $dir Sort Direction * * @return array ordered arrays/static function Sortbycol ($ar
Ray, $keyname, $dir = sort_asc) {return self::sortbymulticols ($array, Array ($keyname => $dir)); /** * Sorts a two-dimensional array by multiple columns, similar to the order by * usage in SQL statements: * @code php * $rows = Arrayhelper::sortbymulticols (
$rows, Array (* ' Parent ' => sort_asc, * ' name ' => sort_desc, *)); * @endcode * * @param array $rowset arrays to be sorted * @param array $args sort the keys * * The array ordered by @return arrays/Stati
C function Sortbymulticols ($rowset, $args) {$sortArray = array ();
$sortRule = '; foreach ($args as $sortField => $sortDir) {foreach ($rowset as $offset => $row) {$sortArra
y[$sortField] [$offset] = $row [$sortField]; $sortRule. = ' $sortArray [\ '. $sortField. '\'], ' . $sortDir.
', ';
} if (Empty ($sortArray) | | | empty ($sortRule)) {return $rowset; } EvaL (' Array_multisort, $sortRule.
' $rowset);
return $rowset;
}
}
I hope this article will help you with your PHP programming.