Tutorial on array operation class _ PHP

Source: Internet
Author: User
Array operation class. RemoveEmpty (, (: removeEmpty ([([] getCols (, ([] [toHashmap (,, ([[] [([] groupBy (, ([] [] toTree (,,
RemoveEmpty (&, = (=> (: removeEmpty ([= (= ''([] = 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 ** @ param array $ arr data source * @ param string $ col the key to be queried ** @ return array contains the array getCols (, = ([[] = [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 the 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 Key value of g $ valueField ** @ return array the converted HashMap style array toHashmap (, = ([] = [([] = 1, 'value' => '1-1', 'parent' => 1), * array ('id' => 2, 'value' => '2-1 ', 'parent' => 1), * array ('id' => 3, 'value' => '3-1', 'parent' => 1 ), * array ('id' => 4, 'value' => '4-1', 'parent' => 2), * array ('id' => 5, 'value' => '5-1', 'parent' => 2), * array ('id' => 6, 'value' => '6-1 ', 'parent' => 3 ),* ); * $ Values = ArrayHelper: groupBy ($ rows, 'parent'); ** dump ($ values ); * // The output result is * // array (* // 1 => array (* // array ('id' => 1, 'value' => '1-1', 'parent' => 1), * // array ('id' => 2, 'value' => '2-1', 'parent' => 1), * // array ('id' => 3, 'value' => '3-1', 'parent' => 1 ),*//), * // 2 => array (* // array ('id' => 4, 'value' => '4-1', 'parent' => 2 ), * // array ('id' => 5, 'VA Lue '=> '5-1', 'parent' => 2 ),*//), * // 3 => array (* // array ('id' => 6, 'value' => '6-1', 'parent' => 3 ), *//),*//) * @ endcode ** @ param array $ arr data source * @ param string $ keyField is the key name for the group. ** @ return array: the result of the group groupBy (, = (= [[] [] = 1, 'value' => '1-1', 'parent' => 0), * array ('id' => 2, 'value' => '2-1', 'parent' => 0), * array ('id' => 3, 'value' => '3-1 ', 'parent' => 0), ** arra Y ('id' => 7, 'value' => '2-1-1 ', 'parent' => 2), * array ('id' => 8, 'value' => '2-1-2 ', 'parent' => 2), * array ('id' => 9, 'value' => '3-1-1 ', 'parent' => 3), * array ('id' => 10, 'value' => '3-1-1-1 ', 'parent' => 9 ),*); ** $ tree = ArrayHelper: tree ($ rows, 'id', 'parent', 'nodes '); ** dump ($ tree ); * // The output result is: * // array ('id' => 1 ,..., 'nodes '=> array (), * // array ('id' => 2 ,..., 'Nodes '=> array (* // array (..., 'parent' => 2, 'nodes '=> array (), * // array (..., 'parent' => 2, 'nodes '=> array (), * //), * // array ('id' => 3 ,..., 'nodes '=> array (* // array ('id' => 9 ,..., 'parent' => 3, 'nodes '=> array (* // array (...,, 'parent' => 9, 'nodes '=> array (),*//),*//),*//) * @ endcode ** to obtain the subtree with any node as the root, you can use the $ refs parameter: * @ code php * $ refs = null; * $ tree = Array Helper: 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 toTree (,, = 'parent _ id', = 'childrens', & == (=> [] [] = [] = & [= (=> = [((! ([[] = & [= & [] [] = & [[] = & [TreeToArray (, = 'childrens' = ([]) & ([[] = (, self: treeToArray (, ([] = [] = 1, 'value' => '1-1 ', 'parent' => 1), * array ('id' => 2, 'value' => '2-1', 'parent' => 1 ), * array ('id' => 3, 'value' => '3-1', 'parent' => 1), * array ('id' => 4, 'value' => '4-1', 'parent' => 2), * array ('id' => 5, 'value' => '5-1 ', 'parent' => 2), * array ('id' => 6, 'value' => '6-1', 'parent' => 3 ),*); ** $ rows = ArrayHelper: sortByCol ($ rows, 'id', SORT_DESC); * dump ($ rows); * // The output result is: * // array ('id' => 6, 'value' => '6-1', 'parent' => 3 ), * // array ('id' => 5, 'value' => '5-1', 'parent' => 2 ), * // array ('id' => 4, 'value' => '4-1', 'parent' => 2 ), * // array ('id' => 3, 'value' => '3-1', 'parent' => 1 ), * // array ('id' => 2, 'value' => '2-1', 'parent' => 1 ), * // array ('id' => 1, 'value' => '1-1', 'parent' => 1 ),*//) * @ endcode ** @ param array $ array the array to be sorted * @ param string $ keyname the sort key * @ param int $ dir sort direction ** @ return array the sorted array sortByCol (,, = self: sortByMultiCols (, (=> SORT_ASC, * 'name' => SORT_DESC ,*)); * @ endcode ** @ param array $ array to be sorted * @ param array $ args sort key ** @ return array sortByMultiCols (, = ''(=> [] [] = [. = '$ sortArray [\''.. '\'], '.. ',' () | ('Array _ multisort ('.. '$ rowset );'

Http://www.bkjia.com/PHPjc/440429.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/440429.htmlTechArticleremoveEmpty (, = (: removeEmpty ([= (= ''([] = getCols (, = ([[] = [toHashmap (, = ([] = [([] = groupBy (, = (= [[] [] = toTree (, =...

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.