Home > php教程 > php手册 > body text

数组操作类

WBOY
Release: 2016-06-13 11:28:36
Original
1144 people have browsed it

<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>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template