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

CI中几个数组辅助函数

时间:2014-07-22 14:49来源: 作者: 点击:
分享到:
感觉太少了,看我之前整理的一个:http://www.oschina.net/code/snippet_272777_22661
感觉太少了,看我之前整理的一个:http://www.oschina.net/code/snippet_272777_22661
/**
 * 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;
	}
}
精彩图集

赞助商链接