PHP Array Tool type

Source: Internet
Author: User
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) $OUTPU t[$k] [$NDARR [$ndkey]] = $ndval;} else $output [$k] = Explode (",", $v); }return $output;} $array 2D = Array (' First ' =>array (' title ' = ' 1111 ', ' date ' = ' 2222 '), ' second ' =>array (' title ' = ' 1111 ', ' Date ' = ' 2222 '), ' third ' =>array (' title ' = ' 2222 ', ' date ' = ' 3333 '); echo "
";p Rint_r (unique_array_2d ($array 2d,true)),/*** the two-dimensional array through the specified key to go to the @param array $arr to go to the heavy arrays of * @param the $by array to specify key to go to the weight, the parameter Does not specify that returns an 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 ' = ' John Doe '), a Rray (' id ' = + 124, ' name ' = ' Harry '), Array (' id ' = ' = ', ' 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 ')));p Rint_r (AR Ray_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 (array (' ID ' =>1, ' num ' =>10, ' type ' = ' news '), 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; }}}}/** * Returns 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;} /** * Convert a two-dimensional array to HashMap * * If the $valueField argument is omitted, each entry is an array containing all the data for that 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 * @param string $keyField * * @return array */function array_group_by (&    $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 according to the specified field * * When the $returnReferences parameter is true, the tree field that returns the result is the trees, and the refs field is the node reference. * With the returned node reference, it is convenient to get a subtree containing the root of any node. * * @param array $arr raw data * @param string $fid Node ID field name * @param string $fparent node parent ID field name * @param string $fchildrens Save sub-section The field name of the point * @param boolean $returnReferences whether to include the 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 [$offset];    } $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, ' refs ' = $pkvRefs);    } else {return $tree; }}/** * Convert a tree to a flat array * * @param array $node * @param string $fchildrens * * @return array */function tree_to_array (& $no    DE, $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;} /** * Sorts the array according to the specified key value * * @param arrays $array to be sorted * @param string $keyname key value name * @param int $sortDirection Sort direction * * @retu RN Array */function Array_column_sort($array, $keyname, $sortDirection = SORT_ASC) {return Array_sortby_multifields ($array, array ($keyname = = $sortDirection));} /** * A two-dimensional array is sorted by the specified column, similar to the ORDER by * * In the SQL statement @param array $rowset * @param array $args */function array_sortby_multifie    LDS ($rowset, $args) {$sortArray = array ();    $sortRule = "; foreach ($args as $sortField + $sortDir) {foreach ($rowset as $offset + = $row) {$sortArray [$sor        tfield][$offset] = $row [$sortField]; } $sortRule. = ' $sortArray [\ '. $sortField. '\'], ' . $sortDir.    ', ';    if (Empty ($sortArray) | | empty ($sortRule)) {return $rowset;} Eval (' Array_multisort ('. $sortRule.    ' $rowset); return $rowset;} /*description: The position of the two elements in the swap array, the elements include key and value, as shown in the following example $arr = Array (one-to-one ', ' X ', ' B ', ' X ', ' C ', + = ' d ') ; $res = Array_exchange ($arr, one, one); */function Array_exchange ($arr, $arg 1, $arg 2) {$r = range (0, COUNT ($arr)-1 ); $res = $res _bak = Array_combine ($r, Array_Keys ($arr)), $change = Array ($arg 1, $arg 2), 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 $array;} /* Suppose: Given a large array and a string, it is required to find 5 elements larger than the string in this large array. For a decimal group (for example, dozens of elements below the array), you can use a circular method to compare one, but for large arrays, this method is obviously not, it needs to find a quick find the location of the solution. $search = ' arr '; $array = Array (' ABS ', ' ACOs ', ' Acosh ', ' addcslashes ', ' addslashes ', ' aggregate_info ', ' Array_diff ', ' Array_fill_keys ', ' Array_fill ', ' array_filter ', ' base64_encode ', ' basename ', ' bcadd ', ' bccomp ', '; $arrpos = Array_pos ($   Array, $search);  Positioning $arr = Array_slice ($array, $arrpos, 5); Take out the array print_r ($arr); Quick Find results: Array ([0] = Array_diff [1] = Array_fill_keys [2] = = Array_fill [3] =&G T 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 data convert * @param array/string $data array * @param string $input encoding to convert * @param string $output converted Encoding */function Array_iconv ($data, $input = ' GBK ', $output = ' utf-8 ') {if (!is_array ($data)) {return Iconv ($input, $output, $da TA);} 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 string to array * * @paramstring $data String * @returnarray returns an array format, if data is empty, returns an empty array */func tion String2array ($data) {if ($data = = ') return array (), eval ("\ $array = $data;"); return $array;} /** * Returns a string or array that has been processed by stripslashes * @param $string the string or array to be processed * @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 an array to a string * * @paramarray $data array * @parambool $isformdata if 0, new_stripslashes processing is not used, optional parameter, default is 1* @ Returnstring returns a string, if data is empty, returns an empty */function array2string ($data, $isformdata = 1) {if ($data = = ") return"; if ($ Isformdata) $data = New_stripslashes ($data); return addslashes (Var_export ($data, TRUE));}               /* * Array converted to object $array = Array (' name ' = ' = ' one ', ' sex ' = ' '-' ', ' ' 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;} /* * Object Conversion array */function Object2array ($objParam) {$obj _param = (array) $objParam; foreach ($obj _param as $key + = $va Lue) {if (Is_object ($value)) {Object2array ($value), $obj _param [$key] = (array) $value;}} return $obj _param;} Adding elements at the beginning of the array keeps the original key constant 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) {Array_push ($arr _keys, $key); array_p Ush ($arr _values, $value);} $arr _result = Array_combine ($arr _keys, $arr _values); return $arr _result;} Gets the value under a specific key under a multidimensional array and generates a one-dimensional array of function Getkey2array (array $arr, $key) {if (!trim ($key)) return False;preg_match_all ("/\ "$key \"; \w{1}:(?: \ d+:|) (. *);/", Serialize ($arr), $output); return $output [1];} Multidimensional array to one-dimensional array $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.