Php array tool-php Tutorial

Source: Internet
Author: User
Php array tool Class PHP two-dimensional array deduplication function & lt ;? Phpfunctionunique_array_2d ($ array2D, $ stkeepfalse, $ ndformattrue) {judge whether to retain the primary array key (the primary array key can be non-numeric) if ($ stkeep) $ stArrarray_keys ($ array2D ); determine whether to preserve the php array tool class
PHP two-dimensional array deduplication function
 $ 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' => '123', 'date' => '123 '), 'second' => array ('title' => '123', 'date' => '123 '), 'third' => array ('title' => '123456', 'date' => '123456'); echo"
"; Print_r (unique_array_2d ($ array2D, true )); /*** remove a two-dimensional array by specifying the key ** @ param array $ arr the array to be duplicated * @ param array $ by specifies the key deduplication, if this parameter is not specified, array () * @ return array */function array_multunique ($ arr, $ by = array () {$ temp = array (); foreach ($ arr as $ key => $ val) {foreach ($ by as $ v) {$ temp [$ key]. = isset ($ val [$ v])? $ Val [$ v]: '';} return array_intersect_key ($ arr, array_unique ($ temp ));} /* $ aa = array ('id' => 123, 'name' => 'Zhang San'), array ('id' => 123, 'name' => 'Li si'), array ('id' => 124, 'name' => 'Wang 5'), array ('id' => 125, 'name' => 'Zhao Liu'), array ('id' => 126, 'name' => 'Zhao Liu'); $ key = 'id '; array_multunique ($ aa, array ('id'); */function array_remove_key ($ array, $ keys) {if (! Is_array ($ array) |! Is_array ($ keys) {return false;} foreach ($ array as $ t) {foreach ($ keys as $ k) {unset ($ t [$ k]);} $ doc [] = $ t;} return $ doc;}/* $ array = array ('0' => array ('a' => 'aaaaa ', 'B' => 'bbbbb', 'C' => array ('D' => 'ddddd ', 'E' => 'eeeee ')), '1' => array ('a' => 'aaaaa', 'B' => 'bbbbb ', 'C' => array ('D' => 'ddddd ', 'E' => 'eeeee'); print_r (array_remove_key ($ array, array ('C'); */function array_remove_key_val (& $ a, $ B, $ c) {foreach ($ a as $ key => $ value) {if (isset ($ value [$ B]) & ($ value [$ B] = $ c )) {unset ($ a [$ key]) ;}}/ * $ a = array ('id' => 1, 'num' => 10, 'type' => 'new'), array ('id' => 2, 'num' => 100, 'type' => 'Pic ')); print_r (array_remove_key_val ($ a, "id", "1 "));*/
Array <=> xml mutual conversion http://hudeyong926.iteye.com/blog/836048
  $ Value) {if (is_array ($ value) {array_remove_empty ($ arr [$ key]);} else {$ value = trim ($ value ); if ($ value = '') {unset ($ arr [$ key]);} elseif ($ trim) {$ arr [$ key] = $ value ;}}}} /*** return all values of the specified key from a two-dimensional array ** @ param array $ arr * @ param string $ col ** @ return array */function array_col_values (& $ arr, $ col) {$ ret = array (); foreach ($ arr as $ row) {if (isset ($ row [$ col]) {$ ret [] = $ row [$ col] ;}} Return $ ret;}/*** converts a two-dimensional array to a hashmap. ** if the $ valueField parameter is omitted, each item in the conversion result is an array containing all the data of the item. ** @ Param array $ arr * @ param string $ keyField * @ param string $ valueField ** @ return array */function array_to_hashmap (& $ 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 ** @ param array $ arr * @ par Am string $ keyField ** @ return array */function array_group_by (& $ 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 fields. ** when the $ returnReferences parameter is true, the tree field of the returned result is a tree, the refs field is referenced by the node. * Using the returned node reference, you can easily obtain the subtree containing any node as the root. ** @ Param array $ arr raw data * @ param string $ fid node ID field name * @ param string $ fparent node parent ID field name * @ param string $ fchildrens stores the field name of the child node * @ param boolean $ whether returnReferences contains a node reference in the returned result ** return array */function array_to_tree ($ arr, $ fid, $ fparent = 'parent _ id', $ fchildrens = 'childrens', $ returnReferences = false) {$ pkvRefs = array (); foreach ($ arr as $ offset => $ row) {$ pkvRefs [$ row [$ fid] = & $ arr [$ off Set] ;}$ tree = array (); foreach ($ arr as $ offset =>$ row) {$ parentId = $ row [$ fparent]; if ($ parentId) {if (! Isset ($ pkvRefs [$ parentId]) {continue ;}$ parent = & $ pkvRefs [$ parentId]; $ parent [$ fchildrens] [] = & $ arr [$ offset];} else {$ tree [] = & $ arr [$ offset] ;}} if ($ returnReferences) {return array ('tree' => $ tree, 'refers' => $ pkvRefs);} else {return $ tree ;}} /*** convert the tree to a flat array ** @ param array $ node * @ param string $ fchildrens ** @ return array */function tree_to_array (& $ node, $ fchildrens = 'childrens ') {$ Ret = array (); if (isset ($ node [$ fchildrens]) & is_array ($ node [$ fchildrens]) {foreach ($ node [$ fchildrens] as $ child) {$ ret = array_merge ($ ret, tree_to_array ($ child, $ fchildrens ));} unset ($ node [$ fchildrens]); $ ret [] = $ node;} else {$ ret [] = $ node;} return $ ret ;} /*** sort arrays by specified key values ** @ param array $ array * @ param string $ keyname key value name * @ param int $ sortDirection ** @ return arra Y */function array_column_sort ($ array, $ keyname, $ sortDirection = SORT_ASC) {return array_sortby_multifields ($ array, array ($ keyname => $ sortDirection ));} /*** sort a two-dimensional array by specified column, similar to order by ** @ param array $ rowset * @ param array $ args */function array_sortby_multifields ($ rowset, $ args) {$ sortArray = array (); $ sortRule = ''; foreach ($ args as $ sortField => $ sortDir) {foreach ($ rowset as $ o Ffset => $ row) {$ sortArray [$ sortField] [$ offset] = $ row [$ sortField];} $ sortRule. = '$ sortArray [\''. $ sortField. '\'], '. $ sortDir. ',';} if (empty ($ sortArray) | empty ($ sortRule) {return $ rowset;} eval ('Array _ multisort ('. $ sortRule. '$ rowset);'); return $ rowset;}/* description: positions of two elements in the array, including key and value, for detailed usage, see the following example $ arr = array (11 => 'A', 22 => 'B', 33 => 'C', 44 => 'D '); $ res = array_e Xchange ($ arr, 11, 33); */function array_exchange ($ arr, $ arg1, $ arg2) {$ r = range (0, count ($ arr) -1); $ res = $ res_bak = array_combine ($ r, array_keys ($ arr); $ change = array ($ arg1, $ arg2 ); list ($ res [array_search ($ change [0], $ res_bak)], $ res [array_search ($ change [1], $ res_bak)]) = array ($ change [1], $ change [0]); foreach ($ res as $ v) {$ array [$ v] = $ arr [$ v];} return $ arra Y;}/* suppose: given a large array and a string, five elements larger than this string must be found in this large array. For a small array (for example, an array with dozens of elements or less), you can use a loop method to compare them one by one. However, for a large array, this method obviously does not work, to solve this problem, you need to find a quick solution. $ Search = 'arr'; $ array = array ('abs ', 'accos', 'acoss', 'addcslashes', 'addslashes ', 'aggregate _ info ', 'array _ diff ', 'Array _ fill_keys', 'Array _ fill', 'Array _ filter', 'base64 _ encoding', 'basename ', 'bcadd ', 'bccomp ',); $ arrpos = array_pos ($ array, $ search); // locate $ arr = array_slice ($ array, $ arrpos, 5 ); // Retrieve the array print_r ($ arr); quick search result: array ([0] => array_diff [1] => array_fill_keys [2] => array_fill [3] => array_filter [4] => Base64_encode) */function array_pos ($ array, $ search) {if (empty ($ array) return false; if (! $ Search) return 0; sort ($ array); $ array_turn = array_flip ($ array); if (isset ($ array_turn [$ search]) {$ arrpos = $ array_turn [$ search];} else {$ tmp_arr = $ array; $ tmp_arr [] = $ search; sort ($ tmp_arr ); $ tmp_arr_turn = array_flip ($ tmp_arr); $ arrpos = $ tmp_arr_turn [$ search];} return $ arrpos ;} /*** encode and convert data * @ param array/string $ data array * @ param string $ encoding to be converted by input * @ param string $ encoding after output */ function array _ Iconv ($ data, $ input = 'gbk', $ output = 'utf-8') {if (! Is_array ($ data) {return iconv ($ input, $ output, $ data);} else {foreach ($ data as $ key => $ val) {if (is_array ($ val) {$ data [$ key] = array_iconv ($ val, $ input, $ output );} else {$ data [$ key] = iconv ($ input, $ output, $ val) ;}} return $ data ;}} /** $ str = "array ('workflowid' =>'', 'ishtml '=> '0', 'content _ ishtml' => '0 ', 'create _ to_html_root '=> '0',) "; $ setting = string2array ($ str); $ setting: Array ([workflowid] => [Ishtml] => 0 [content_ishtml] => 0 [create_to_html_root] => 0) ** // *** convert a string to an array ** @ paramstring $ data string * @ returnarray returns the array format. if data is empty, returns an empty array */function string2array ($ data) {if ($ data = '') return array (); eval (" \ $ array = $ data ;"); return $ array ;} /*** returns the string or array processed by stripslashes * @ param $ string or the array * @ return mixed */function new_stripslashes ($ string) {if (! Is_array ($ string) return stripslashes ($ string); foreach ($ string as $ key => $ val) $ string [$ key] = new_stripslashes ($ val ); return $ string;}/*** convert the array to a string ** @ paramarray $ data array * @ parambool $ isformdata if it is 0, new_stripslashes is not used. optional parameter, the default value is 1 * @ returnstring. if data is null, null */function array2string ($ data, $ isformdata = 1) is returned) {if ($ data = '') return''; if ($ isformdata) $ data = new_stripslashes ($ data); return addsl Ashes (var_export ($ data, TRUE);}/** convert an array to an object $ array = array ('name' => 'one ', 'Sex '=> 'two', 'test' => array ('a' => 'SS', 'DD'), 'old' => 'Three '); $ arrayobject = array2Object ($ array); echo $ arrayobject-> name; // oneprint_r (object2Array ($ arrayobject); */function array2Object ($ array) {if (! Is_array ($ array) return $ array; $ object = new stdClass (); if (is_array ($ array) & count ($ array)> 0) {foreach ($ array as $ name = >$ value) {$ name = strtolower (trim ($ name); if ($ name) $ object-> $ name = array2Object ($ value);} return $ object;} else return FALSE;}/** convert an object to an array */function object2Array ($ objParam) {$ obj_param = (array) $ objParam; foreach ($ obj_param as $ key => $ value) {if (is_object ($ value) {o Bject2Array ($ value); $ obj_param [$ key] = (array) $ value ;}return $ obj_param ;} // add elements at the beginning of the array to keep the original key unchanged. arrayUnshift ($ arr, array ('test' => '4') function arrayUnshift ($ arrParams, $ arrAdd) {$ arr_keys = array (); $ arr_values = array (); $ arr_add_keys = array_keys ($ arrAdd); $ arr_keys = array ($ arr_add_keys [0]); $ arr_values = array ($ arrAdd [$ arr_add_keys [0]); foreach ($ arrParams as $ key => $ value) {Rray_push ($ arr_keys, $ key); array_push ($ arr_values, $ value);} $ arr_result = array_combine ($ arr_keys, $ arr_values); return $ arr_result ;} // obtain the value of a specific key in a multi-dimensional array and generate a one-dimensional array function getKey2Array (array $ arr, $ key) {if (! Trim ($ key) return false; preg_match_all ("/\" $ key \ "; \ w {1 }:(?: \ D +: | )(.*?); /", Serialize ($ arr), $ output); return $ output [1];} // Convert multi-dimensional arrays to one-dimensional arrays $arr=array('123.html','456.html',array('dw.html','fl.html',array('ps.html','fw.html')),' AB .html '); function rebuild_array ($ arr) {// rebuild a arraystatic $ tmp = array (); for ($ I = 0; $ I
  
   

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.