PHP array Operation class instance, PHP array Instance _php tutorial

Source: Internet
Author: User

PHP array Operation class instance, PHP array instance


The examples in this article describe the PHP array manipulation classes. Share to everyone for your reference. Specific as follows:

Class arrayhelper{/** * Remove blank elements from the array (including elements with only whitespace characters) * Usage: * @code php * $arr = Array (', ' test ', ');   * Arrayhelper::removeempty ($arr);   * * Dump ($arr); *//output will be only ' test ' * @endcode * * @param array $arr to be processed @param boolean $trim whether to call the trim function on an array element */Stati       C 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); *//output result is *//Array (*//' 1-1 ', *//' 2-1 ', *//) * @eNdcode * * @param array $arr Data source * @param string $col the key to query * * @return array containing 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 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 $valueField   parameter, the result of the conversion is an array that contains all the data for the item. * Usage 2: * @code php * $rows = Array (* Array (' id ' = + 1, ' value ' = ' 1-1 '), * array (' id ' = 2, ' Val   UE ' = ' 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 The value of the key to convert * @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 The key name to group by    * * @return The result of array grouping */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 get a subtree of any node as root, you can use $refs parameters: * @code php * $refs= NULL;   * $tree = Arrayhelper::tree ($rows, ' id ', ' parent ', ' nodes ', $refs);   *//Output ID 3 node and all 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 The field name of the child node is saved * @param boolean $refs whether to include a node reference in the returned result * * return array of arrays of tree structure */static FU  Nction 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 flat array * This method is the inverse of the trees () method. * * @param array $tree tree array * @param string $keyChildrens The key name that contains the child nodes * * @return Array expanded arrays */static function    Treetoarray ($tree, $keyChildrens = ' childrens ') {$ret = array (); if (Isset ($tree [$keyChildrens]) && is_array ($tree [$keyChildrens]) {foreach ($tree [$keyChildrens] as $chi      LD) {$ret = Array_merge ($ret, Self::treetoarray ($child, $keyChildrens));      } unset ($node [$keyChildrens]);    $ret [] = $tree;    } else {$ret [] = $tree;  } return $ret; /** * Sort by the specified key * * @endcode * * @param array $array the array to sort * @param string $keyname sort the key * @param int $dir Sort Direction * * @return Array sorted arrays */static function Sortbycol ($array, $keyname, $dir = SORT_ASC) {return sel  F::sortbymulticols ($array, array ($keyname = $dir)); }/** * A two-dimensional array in accordance with theMore than one column is sorted, like order by * in SQL statements usage: * @code php * $rows = Arrayhelper::sortbymulticols ($rows, Array (* ' Paren   T ' = = SORT_ASC, * ' name ' = + sort_desc, *)); * @endcode * * @param array $rowset arrays to sort * @param array $args sorted keys * * @return array sorted arrays */static FUNCT    Ion Sortbymulticols ($rowset, $args) {$sortArray = array ();    $sortRule = "; foreach ($args as $sortField + $sortDir) {foreach ($rowset as $offset + = $row) {$sortArray [$s      ortfield][$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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1030432.html www.bkjia.com true http://www.bkjia.com/PHPjc/1030432.html techarticle PHP array operation class instance, PHP array instance This article describes the PHP array operation class. Share to everyone for your reference. The following: Class arrayhelper{/** * Remove white space from the array ...

  • Related Article

    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.