PHP array operation class instance, php array instance
This example describes the PHP array operation class. Share it with you for your reference. The details are as follows:
Class ArrayHelper {/*** Delete blank elements from the array (including elements with only blank characters) ** usage: * @ code php * $ arr = array ('', 'test', ''); * ArrayHelper: removeEmpty ($ arr); ** dump ($ arr ); * // In the output result, only 'test' * @ endcode ** @ param array $ array to be processed by arr * @ param boolean $ whether trim calls the trim function */static function removeEmpty (& $ arr, $ trim = TRUE) {foreach ($ arr as $ key => $ value) {if (is_array ($ 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); * // The output result is * // array (* // '1-1 ', * // '2-1', * //) * @ endcode ** @ p Aram array $ arr data source * @ param string $ col the key to be queried ** @ return array contains an array of all values of 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 into a 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 ); * // The output result is * // array (* // 1 => '1-1', * // 2 => '2-1 ',*//) * @ endcode ** if the $ valueField parameter is omitted, each item in the conversion result is an array containing all the data of this 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); * // The 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 $ the key value of keyField for conversion * @ param strin G $ valueField key value ** @ return array converted HashMap style array */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;}/*** groups a two-dimensional array by the value of the specified field ** usage: * @ endcode ** @ param array $ arr data source * @ param string $ ke The key name ** @ return array result */static function groupBy ($ arr, $ keyField) {$ ret = array (); foreach ($ arr as $ row) {$ key = $ row [$ keyField]; $ ret [$ key] [] = $ row;} return $ ret ;} /*** convert a two-dimensional array of a plane to a tree structure based on the specified field *** to obtain the subtree with any node as the root, you can use the $ refs parameter: * @ code php * $ refs = null; * $ tree = ArrayHelper: tree ($ rows, 'id', 'parent', 'nodes ', $ refs ); ** // The node whose output id is 3 and all its subnodes ** $ 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 field name * @ param boolean $ whether refs contains a node reference in the returned result ** return array */static function toTree ($ arr, $ keyNodeId, $ keyParentId = 'parent _ id', $ keyChildrens = 'childrens', & $ refs = NULL) {$ refs = array (); foreach ($ arr as $ offset => $ row) {$ Rr [$ 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 ;} /*** split the tree array into a flat array ** this method is the inverse operation of the tree () method. ** @ Param array $ tree-like array * @ param string $ keyChildrens contains the key name of the subnode ** @ return array expanded array */static function treeToArray ($ tree, $ keyChildrens = 'childrens') {$ ret = array (); if (isset ($ tree [$ keyChildrens]) & is_array ($ tree [$ keyChildrens]) {foreach ($ tree [$ keyChildrens] as $ child) {$ ret = array_merge ($ ret, self: treeToArray ($ child, $ keyChildrens ));} unset ($ node [$ keyChildrens]); $ ret [] = $ tree;} else {$ ret [] = $ tree;} return $ ret ;} /*** sort arrays by specified keys ** @ endcode ** @ param array $ array * @ param string $ key for sorting by keyname * @ param int $ dir sorting direction ** @ return array */static function sortByCol ($ array, $ keyname, $ dir = SORT_ASC) {return self: sortByMultiCols ($ array, array ($ keyname => $ dir ));} /*** sort a two-dimensional array BY multiple columns, similar to order by ** in SQL statements: * @ code php * $ rows = ArrayHelper :: sortByMultiCols ($ rows, array (* 'parent' => SORT_ASC, * 'name' => SORT_DESC ,*)); * @ endcode ** @ param array $ array to be sorted * @ param array $ args sort key ** @ return array sorted array */static function sortByMultiCols ($ rowset, $ args) {$ sortArray = array (); $ sortRule = ''; foreach ($ args as $ sortField => $ sortDir) {foreach ($ rowset as $ offset => $ row) {$ sortArray [$ 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 php programming.