<commit_before><?php
/**
 * Array
 *
 * Copyright (c) 2008-2011 <NAME>. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @package     Qwin
 * @subpackage  Util
 * @author      <NAME> <<EMAIL>>
 * @copyright   <NAME>
 * @license     http://www.opensource.org/licenses/apache2.0.php Apache License
 * @version     $Id$
 * @since       2009-11-24 20:45:11
 */

class Qwin_Util_Array
{
    /**
     * 将回调函数作用到给定数组的各级单元上,功能同array_map
     *
     * @param array|string $callback 回调方法或函数
     * @param array $array
     * @return array
     * @todo 支持多数组
     */
    public static function multiMap($callback, array $array)
    {
        foreach ($array as $key => $value) {
            if (!is_array($value)) {
                $array[$key] = call_user_func_array($callback, array($value));
            } else {
                $array[$key] = self::multiMap($callback, $array[$key]);
            }
        }
        return $array;
    }

    /**
     * 删除数组中指定的键名
     *
     * @param array $data 二维数组,一般是从数据库读出
     * @param array $keyList 删除的键名
     * @return array
     */
    public static function unsetByKey(array $data, array $keyList)
    {
        $temp = array();
        $keyList = array_flip($keyList);
        foreach($data as $key => $value) {
            $temp[$key] = array_diff_key($value, $keyList);
        }
        return $temp;
    }

    /**
     * 获取由指定的键名组成的数组
     *
     * @param array $data 二维数组,一般是从数据库读出
     * @param array $keyList 指定的键名
     * @return array
     */
    public static function filterByKey(array $data, array $keyList)
    {
        $temp = array();
        $keyList = array_flip($keyList);
        foreach($data as $key => $value) {
            $temp[$key] = array_intersect_key($value, $keyList);
        }
        return $temp;
    }

    /**
     * 判断数组1是不是数组2的子集
     *
     * @param array $array1 数组1
     * @param array $array2 数组2
     * @param string $type
     * @return bool
     * @todo 按键名比较,不按键名比较的几种方式
     */
    public static function isSubset($array1, $array2)
    {
        return array_intersect_assoc($array1, $array2) == $array1;
    }

    /**
     * 根据指定的键名和排列顺序,排列二级数组
     *
     * @param array $list 排列的二级数组
     * @param string $order_key 排序的键名
     * @param int $type 3 => SORT_DESC; 4 => SORT_ASC;
     * @param array $type 排列好的二级数组
     * @todo 可以指定排列顺序,由小到大和由大到小
     */
    public static function orderBy($list, $keyName = 'order', $type = SORT_DESC)
    {
        if (0 == count($list)) {
            return $list;
        }
        $list2 = array();
        foreach ($list as $key => $value) {
            $list2[$key] = $val[$keyName];
        }
        array_multisort($list2, $type, $list);
        return $list;
    }

    /**
     * 按大写字母分割字符串
     *<commit_msg>[框架]修复数组工具类变量错误<commit_after><?php
/**
 * Array
 *
 * Copyright (c) 2008-2011 <NAME>. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @package     Qwin
 * @subpackage  Util
 * @author      <NAME> <<EMAIL>>
 * @copyright   <NAME>
 * @license     http://www.opensource.org/licenses/apache2.0.php Apache License
 * @version     $Id$
 * @since       2009-11-24 20:45:11
 */

class Qwin_Util_Array
{
    /**
     * 将回调函数作用到给定数组的各级单元上,功能同array_map
     *
     * @param array|string $callback 回调方法或函数
     * @param array $array
     * @return array
     * @todo 支持多数组
     */
    public static function multiMap($callback, array $array)
    {
        foreach ($array as $key => $value) {
            if (!is_array($value)) {
                $array[$key] = call_user_func_array($callback, array($value));
            } else {
                $array[$key] = self::multiMap($callback, $array[$key]);
            }
        }
        return $array;
    }

    /**
     * 删除数组中指定的键名
     *
     * @param array $data 二维数组,一般是从数据库读出
     * @param array $keyList 删除的键名
     * @return array
     */
    public static function unsetByKey(array $data, array $keyList)
    {
        $temp = array();
        $keyList = array_flip($keyList);
        foreach($data as $key => $value) {
            $temp[$key] = array_diff_key($value, $keyList);
        }
        return $temp;
    }

    /**
     * 获取由指定的键名组成的数组
     *
     * @param array $data 二维数组,一般是从数据库读出
     * @param array $keyList 指定的键名
     * @return array
     */
    public static function filterByKey(array $data, array $keyList)
    {
        $temp = array();
        $keyList = array_flip($keyList);
        foreach($data as $key => $value) {
            $temp[$key] = array_intersect_key($value, $keyList);
        }
        return $temp;
    }

    /**
     * 判断数组1是不是数组2的子集
     *
     * @param array $array1 数组1
     * @param array $array2 数组2
     * @param string $type
     * @return bool
     * @todo 按键名比较,不按键名比较的几种方式
     */
    public static function isSubset($array1, $array2)
    {
        return array_intersect_assoc($array1, $array2) == $array1;
    }

    /**
     * 根据指定的键名和排列顺序,排列二级数组
     *
     * @param array $list 排列的二级数组
     * @param string $order_key 排序的键名
     * @param int $type 3 => SORT_DESC; 4 => SORT_ASC;
     * @param array $type 排列好的二级数组
     * @todo 可以指定排列顺序,由小到大和由大到小
     */
    public static function orderBy($list, $keyName = 'order', $type = SORT_DESC)
    {
        if (0 == count($list)) {
            return $list;
        }
        $list2 = array();
        foreach ($list as $key => $value) {
            $list2[$key] = $value[$keyName];
        }
        array_multisort($list2, $type, $list);
        return $list;
    }

    /**
     * 按大写字母分割字符串
     *
{'fraction_non_alphanumeric': 0.12599145820622332, 'fraction_numerical': 0.013727882855399633, 'mean_word_length': 2.153920153920154, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1274258', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2426, 'n_words': 556}