龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php CI中几个数组辅助函数

时间:2014-10-06 02:09来源:网络整理 作者:网络 点击:
分享到:
CI中几个数组辅助函数 [代码片段(81行)]
/**
 * Element
 *
 * 取数组中的元素,判断键名是否存在并且判断值是否存在
 *
 */
if ( ! function_exists('element'))
{
    function element($item, $array, $default = FALSE)
    {
        if ( ! isset($array[$item]) OR $array[$item] == "")
        {
            return $default;
        }

        return $array[$item];
    }
}

// ------------------------------------------------------------------------

/**
 * 随机返回数组内的一个函数,用array_rand()获取随机键名
 *
 * @access  public
 * @param   array
 * @return  mixed   depends on what the array contains
 */
if ( ! function_exists('random_element'))
{
    function random_element($array)
    {
        if ( ! is_array($array))
        {
            return $array;
        }

        return $array[array_rand($array)];
    }
}

// --------------------------------------------------------------------

/**
 * Elements
 *
 * 从数组中返回若干单元,若数组不存在则返回false
 *
 * @access  public
 * @param   array
 * @param   array
 * @param   mixed
 * @return  mixed   depends on what the array contains
 */
if ( ! function_exists('elements'))
{
    function elements($items, $array, $default = FALSE)
    {
        $return = array();

        if ( ! is_array($items))
        {
            $items = array($items);
        }

        foreach ($items as $item)
        {
            if (isset($array[$item]))
            {
                $return[$item] = $array[$item];
            }
            else
            {
                $return[$item] = $default;
            }
        }

        return $return;
    }
}
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接