<span class</span><span ArrayHelper{ </span><span /*</span><span * * 从数组中删除空白的元素(包括只有空白字符的元素) * * 用法: * @code php * $arr = array('', 'test', ' '); * ArrayHelper::removeEmpty($arr); * * dump($arr); * // 输出结果中将只有 'test' * @endcode * * @param array $arr 要处理的数组 * @param boolean $trim 是否对数组元素调用 trim 函数 </span><span */</span> <span static</span> <span function</span> removeEmpty(& <span $arr</span>, <span $trim</span> = <span TRUE</span><span ) { </span><span foreach</span> (<span $arr</span> <span as</span> <span $key</span> => <span $value</span><span ) { </span><span if</span> (<span is_array</span>(<span $value</span><span )) { self</span>::removeEmpty(<span $arr</span>[<span $key</span><span ]); } </span><span else</span><span { </span><span $value</span> = <span trim</span>(<span $value</span><span ); </span><span if</span> (<span $value</span> == ''<span ) { </span><span unset</span>(<span $arr</span>[<span $key</span><span ]); } </span><span elseif</span> (<span $trim</span><span ) { </span><span $arr</span>[<span $key</span>] = <span $value</span><span ; } } } } </span><span /*</span><span * * 从一个二维数组中返回指定键的所有值 * * 用法: * @code php * $rows = array( * array('id' => 1, 'value' => '1-1'), * array('id' => 2, 'value' => '2-1'), * ); * $values = ArrayHelper::getCols($rows, 'value'); * * dump($values); * // 输出结果为 * // array( * // '1-1', * // '2-1', * // ) * @endcode * * @param array $arr 数据源 * @param string $col 要查询的键 * * @return array 包含指定键所有值的数组 </span><span */</span> <span static</span> <span function</span> getCols(<span $arr</span>, <span $col</span><span ) { </span><span $ret</span> = <span array</span><span (); </span><span foreach</span> (<span $arr</span> <span as</span> <span $row</span><span ) { </span><span if</span> (<span isset</span>(<span $row</span>[<span $col</span><span ])) { </span><span $ret</span>[] = <span $row</span>[<span $col</span><span ]; } } </span><span return</span> <span $ret</span><span ; } </span><span /*</span><span * * 将一个二维数组转换为 HashMap,并返回结果 * * 用法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); * // 输出结果为 * // array( * // 1 => '1-1', * // 2 => '2-1', * // ) * @endcode * * 如果省略 $valueField 参数,则转换结果每一项为包含该项所有数据的数组。 * * 用法2: * @code php * $rows = array( * array('id' => 1, 'value' => '1-1'), * array('id' => 2, 'value' => '2-1'), * ); * $hashmap = ArrayHelper::toHashmap($rows, 'id'); * * dump($hashmap); * // 输出结果为 * // array( * // 1 => array('id' => 1, 'value' => '1-1'), * // 2 => array('id' => 2, 'value' => '2-1'), * // ) * @endcode * * @param array $arr 数据源 * @param string $keyField 按照什么键的值进行转换 * @param string $valueField 对应的键值 * * @return array 转换后的 HashMap 样式数组 </span><span */</span> <span static</span> <span function</span> toHashmap(<span $arr</span>, <span $keyField</span>, <span $valueField</span> = <span NULL</span><span ) { </span><span $ret</span> = <span array</span><span (); </span><span if</span> (<span $valueField</span><span ) { </span><span foreach</span> (<span $arr</span> <span as</span> <span $row</span><span ) { </span><span $ret</span>[<span $row</span>[<span $keyField</span>]] = <span $row</span>[<span $valueField</span><span ]; } } </span><span else</span><span { </span><span foreach</span> (<span $arr</span> <span as</span> <span $row</span><span ) { </span><span $ret</span>[<span $row</span>[<span $keyField</span>]] = <span $row</span><span ; } } </span><span return</span> <span $ret</span><span ; } </span><span /*</span><span * * 将一个二维数组按照指定字段的值分组 * * 用法: * @code php * $rows = 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), * 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); * // 按照 parent 分组的输出结果为 * // 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, 'value' => '5-1', 'parent' => 2), * // ), * // 3 => array( * // array('id' => 6, 'value' => '6-1', 'parent' => 3), * // ), * // ) * @endcode * * @param array $arr 数据源 * @param string $keyField 作为分组依据的键名 * * @return array 分组后的结果 </span><span */</span> <span static</span> <span function</span> groupBy(<span $arr</span>, <span $keyField</span><span ) { </span><span $ret</span> = <span array</span><span (); </span><span foreach</span> (<span $arr</span> <span as</span> <span $row</span><span ) { </span><span $key</span> = <span $row</span>[<span $keyField</span><span ]; </span><span $ret</span>[<span $key</span>][] = <span $row</span><span ; } </span><span return</span> <span $ret</span><span ; } </span><span /*</span><span * * 将一个平面的二维数组按照指定的字段转换为树状结构 * * 用法: * @code php * $rows = array( * array('id' => 1, 'value' => '1-1', 'parent' => 0), * array('id' => 2, 'value' => '2-1', 'parent' => 0), * array('id' => 3, 'value' => '3-1', 'parent' => 0), * * array('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); * // 输出结果为: * // array( * // 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 * * 如果要获得任意节点为根的子树,可以使用 $refs 参数: * @code php * $refs = null; * $tree = ArrayHelper::tree($rows, 'id', 'parent', 'nodes', $refs); * * // 输出 id 为 3 的节点及其所有子节点 * $id = 3; * dump($refs[$id]); * @endcode * * @param array $arr 数据源 * @param string $keyNodeId 节点ID字段名 * @param string $keyParentId 节点父ID字段名 * @param string $keyChildrens 保存子节点的字段名 * @param boolean $refs 是否在返回结果中包含节点引用 * * return array 树形结构的数组 </span><span */</span> <span static</span> <span function</span> toTree(<span $arr</span>, <span $keyNodeId</span>, <span $keyParentId</span> = 'parent_id', <span $keyChildrens</span> = 'childrens', & <span $refs</span> = <span NULL</span><span ) { </span><span $refs</span> = <span array</span><span (); </span><span foreach</span> (<span $arr</span> <span as</span> <span $offset</span> => <span $row</span><span ) { </span><span $arr</span>[<span $offset</span>][<span $keyChildrens</span>] = <span array</span><span (); </span><span $refs</span>[<span $row</span>[<span $keyNodeId</span>]] =& <span $arr</span>[<span $offset</span><span ]; } </span><span $tree</span> = <span array</span><span (); </span><span foreach</span> (<span $arr</span> <span as</span> <span $offset</span> => <span $row</span><span ) { </span><span $parentId</span> = <span $row</span>[<span $keyParentId</span><span ]; </span><span if</span> (<span $parentId</span><span ) { </span><span if</span> (!<span isset</span>(<span $refs</span>[<span $parentId</span><span ])) { </span><span $tree</span>[] =& <span $arr</span>[<span $offset</span><span ]; </span><span continue</span><span ; } </span><span $parent</span> =& <span $refs</span>[<span $parentId</span><span ]; </span><span $parent</span>[<span $keyChildrens</span>][] =& <span $arr</span>[<span $offset</span><span ]; } </span><span else</span><span { </span><span $tree</span>[] =& <span $arr</span>[<span $offset</span><span ]; } } </span><span return</span> <span $tree</span><span ; } </span><span /*</span><span * * 将树形数组展开为平面的数组 * * 这个方法是 tree() 方法的逆向操作。 * * @param array $tree 树形数组 * @param string $keyChildrens 包含子节点的键名 * * @return array 展开后的数组 </span><span */</span> <span static</span> <span function</span> treeToArray(<span $tree</span>, <span $keyChildrens</span> = 'childrens'<span ) { </span><span $ret</span> = <span array</span><span (); </span><span if</span> (<span isset</span>(<span $tree</span>[<span $keyChildrens</span>]) && <span is_array</span>(<span $tree</span>[<span $keyChildrens</span><span ])) { </span><span foreach</span> (<span $tree</span>[<span $keyChildrens</span>] <span as</span> <span $child</span><span ) { </span><span $ret</span> = <span array_merge</span>(<span $ret</span>, self::treeToArray(<span $child</span>, <span $keyChildrens</span><span )); } </span><span unset</span>(<span $node</span>[<span $keyChildrens</span><span ]); </span><span $ret</span>[] = <span $tree</span><span ; } </span><span else</span><span { </span><span $ret</span>[] = <span $tree</span><span ; } </span><span return</span> <span $ret</span><span ; } </span><span /*</span><span * * 根据指定的键对数组排序 * * 用法: * @code php * $rows = 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), * 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); * // 输出结果为: * // array( * // 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 要排序的数组 * @param string $keyname 排序的键 * @param int $dir 排序方向 * * @return array 排序后的数组 </span><span */</span> <span static</span> <span function</span> sortByCol(<span $array</span>, <span $keyname</span>, <span $dir</span> =<span SORT_ASC) { </span><span return</span> self::sortByMultiCols(<span $array</span>, <span array</span>(<span $keyname</span> => <span $dir</span><span )); } </span><span /*</span><span * * 将一个二维数组按照多个列进行排序,类似 SQL 语句中的 ORDER BY * * 用法: * @code php * $rows = ArrayHelper::sortByMultiCols($rows, array( * 'parent' => SORT_ASC, * 'name' => SORT_DESC, * )); * @endcode * * @param array $rowset 要排序的数组 * @param array $args 排序的键 * * @return array 排序后的数组 </span><span */</span> <span static</span> <span function</span> sortByMultiCols(<span $rowset</span>, <span $args</span><span ) { </span><span $sortArray</span> = <span array</span><span (); </span><span $sortRule</span> = ''<span ; </span><span foreach</span> (<span $args</span> <span as</span> <span $sortField</span> => <span $sortDir</span><span ) { </span><span foreach</span> (<span $rowset</span> <span as</span> <span $offset</span> => <span $row</span><span ) { </span><span $sortArray</span>[<span $sortField</span>][<span $offset</span>] = <span $row</span>[<span $sortField</span><span ]; } </span><span $sortRule</span> .= '$sortArray[\'' . <span $sortField</span> . '\'], ' . <span $sortDir</span> . ', '<span ; } </span><span if</span> (<span empty</span>(<span $sortArray</span>) || <span empty</span>(<span $sortRule</span><span )) { </span><span return</span> <span $rowset</span><span ; } </span><span eval</span>('array_multisort(' . <span $sortRule</span> . '$rowset);'<span ); </span><span return</span> <span $rowset</span><span ; } }</span>

C语言数组初始化的三种方式:1、在定义时直接赋值,语法“数据类型 arrayName[index] = {值};”;2、利用for循环初始化,语法“for (int i=0;i<3;i++) {arr[i] = i;}”;3、使用memset()函数初始化,语法“memset(arr, 0, sizeof(int) * 3)”。

php求2个数组相同元素的方法:1、创建一个php示例文件;2、定义两个有相同元素的数组;3、使用“array_intersect($array1,$array2)”或“array_intersect_assoc()”方法获取两个数组相同元素即可。

Part1聊聊Python序列类型的本质在本博客中,我们来聊聊探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)的本质。不知道你发现没有,这些类都有一个很明显的共性,都可以用来保存多个数据元素,最主要的功能是:每个类都支持下标(索引)访问该序列的元素,比如使用语法Seq[i]。其实上面每个类都是使用数组这种简单的数据结构表示。但是熟悉Python的读者可能知道这3种数据结构又有一些不同:比如元组和字符串是不能修改的,列表可以

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

增加元素的方法:1、使用unshift()函数在数组开头插入元素;2、使用push()函数在数组末尾插入元素;3、使用concat()函数在数组末尾插入元素;4、使用splice()函数根据数组下标,在任意位置添加元素。

php判断数组里面是否存在某元素的方法:1、通过“in_array”函数在数组中搜索给定的值;2、使用“array_key_exists()”函数判断某个数组中是否存在指定的key;3、使用“array_search()”在数组中查找一个键值。

php去除第一个数组元素的方法:1、新建一个php文件,并创建一个数组;2、使用“array_shift”方法删除数组首个元素;3、通过“print_”r输出数组即可。

元组是固定长度不可变的顺序容器(元素序列),go语言中没有元组类型,数组就相当于元组。在go语言中,数组是一个由固定长度的特定类型元素组成的序列,一个数组可以由零个或多个元素组成;数组的声明语法为“var 数组变量名 [元素数量]Type”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
